I want to get the latest record from a table. My sql is as follow.
Select * from (select * from TABLE_1 WHERE paid=1 order by PAYMENTDATE
DESC) WHERE ROWNUM = 1
I tried to do it with HQL with the mapping pojo classes. It gave errors. My HQL is as follow.
SELECT pay FROM (SELECT payment FROM com.Table1 as payment WHERE
payment.paid=1 ORDER BY payment.paymentDate DESC) as pay where
pay.ROWNUM = 1
The error is,
net.sf.hibernate.QueryException: in expected: SELECT [SELECT pay FROM
(SELECT payment FROM com.TABLE1 as payment WHERE payment.paid=1 ORDER
BY payment.paymentDate DESC) as pay.ROWNUM = 1 ]
Normal queries without subqueries are working.
Eg:
SELECT payment FROM com.Table1 as payment WHERE payment.paid=1
Please help to solve this.
Related
As a programming student, I'm discovering Java (11) & Springboot.
In the application I'm developing, I would like to have a top-sellers list.
It is clear to me how to collect this data in SQL - the query is working effectiv, but using the JPA Repositories & native query it isn't...
Why do I get the error to include the Id column in the "group by" part? If I do this, the result is offcourse not what I was looking for ...
#Query(value = "SELECT *, SUM(od.numberofproducts) AS sold, SUM(p.price * od.numberofproducts) AS revenue FROM OrderDetail od JOIN product p ON p.id = od.productid GROUP BY od.productid ORDER BY sold DESC limit 10", name = "getTopSellersList", nativeQuery=true)
I have an excel file with some data in it (ids) and these id's have more than one record in the database. There are around 400 ids I have and I need to get the latest record for every id. I don't want to do it one by one. I tried using IN clause but it didn't work.
Select *
from myTable with (nolock)
where submission_number IN ('02597', '69875')
order by timestame DESC;
Above query doesn't work what I want. Can some please help/guide?
Thanks
I would do this using apply:
select t.*
from (values ('02597'), ('69875')) v(submission_number) cross apply
(select top (1) t.*
from mytable t
where t.submission_number = v.submission_number
order by t.timestamp desc
) t;
One nice feature is that you can use outer apply, which will return a row in the result set even when there is no match in your table.
Another method that doesn't use a subquery is;
select top (1) with ties t.*
from myTable t
where submission_number in ('02597', '69875')
order by row_number() over (partition by submission_number order by timestame desc);
You can use row_number for that:
select *
from (
select *, row_number() over (partition by submission_number order by timestame desc) rn
from yourtable
) t
where rn = 1
This will return a single record for each submission_number. If you only want the 2 in your in clause, you can add that back as where criteria.
I'm trying to translate a MySQL query to JPA.
I'm using MySQL5 and EclipseLink 2.4.2.
Here's the MySQL query :
SELECT s.id, s.startDate, CAST(GROUP_CONCAT(DISTINCT s.endDate ORDER BY s.date DESC) AS DATE) endDate
FROM table_s s
WHERE ...
GROUP BY s.id, s.startDate
id and date are primary key. They are represented by an embeddable id in JPA and fields are also readable in the entity (insertable/updatable = false).
Here's the JPA query :
SELECT s.id, s.startDate, SQL('CAST(GROUP_CONCAT(DISTINCT ? ORDER BY ? DESC) AS DATE)', s.endDate, s.date) AS endDate
FROM EntityS s
WHERE ...
GROUP BY s.id, s.startDate
The current issue is that the generated sql query does not include s.startDate for a reason I don't know.
If I simply change the code to SELECT s.id, s.startDate, s.endDate, it's working... but I really need to get the last endDate.
I tried MAX(s.endDate) but actually it does not give the last value (even if it currently works, it's not correct and can send a wrong result)
If someone got an idea or a solution, I would be pleased.
Ok, I made it otherwise :
SELECT s.id, s.startDate, s.endDate
FROM EntityS s
WHERE ...
AND s.date = (
SELECT MAX(s.date)
FROM EntityS s2
WHERE s2.id = s.id
AND s2.startDate = s.startDate
AND ...
)
GROUP BY s.id, s.startDate
I've successfully created some JPA 2 criteria queries, but im now converting a query that is too complex for me to be able to convert it. Anyone up for a challenge? I will be eternally thankful for any response! :)
This is the query in plain SQL (mysql):
SELECT o.orderId, oi.insertDate
FROM AIDA_ORDER o
LEFT OUTER JOIN
(SELECT orderId, MIN(insertDate) AS insertDate FROM AIDA_ORDER_INSERT
WHERE insertDate > CURDATE() GROUP BY orderId) AS oi
ON oi.orderId = o.orderId
ORDER BY oi.insertDate, o.orderId;
The AIDA_ORDER table corresponds to a OrderBean, and AIDA_ORDER_INSERT to a OrderInsertBean.
A AIDA_ORDER can have multiple AIDA_ORDER_INSERTS.
Excuse me for anking again about this issue but I need to have a JPA query for this:
select username, count(*)
from Records
group by username
order by count(*) desc
limit 1
I thought about smth like:
select r.username,count(*) from Records r order by r.username desc
and then to call
getResultList().get(0)
but I am allowed to write only:
select r from Records r order by r.username desc
and in this case I do not know how to get what I need.
Does anyone have any idea?
The SQL query has a group by, and orders by count. The JPA query doesn't have any group by and orders by user name. So I don't see how they could return the same thing.
The equivalent JPQL query is
select r.username, count(r.id)
from Record r
group by r.username
order by count(r.id) desc
If you call setMaxResults(1) on the Query object, the limit clause will be added to the generated SQL query, making it completely equivalent.