JPA Query with GROUP BY, HAVING and COUNT - jpa

So the query below is probably not the most efficient, buy still, I am wondering why it is returning no result, even though the SQL counterpart does. There is no error, I am just getting no result. Is it maybe not the correct equivalent for the query I wrote in MySQL?
This is the JPA JPQL.
Query query = em.createQuery("SELECT sub FROM Subscription sub WHERE "
+ "sub.isSuspended = 0 AND "
+ "(SELECT i FROM Invoice i WHERE i.dateDue < CURRENT_DATE AND i.datePaid IS NULL "
+ "GROUP BY i HAVING COUNT(i.idInvoice) > 2) MEMBER OF sub.invoices");
And this is the SQL from MySQL.
SELECT * from subscription
WHERE subscription.is_suspended = 0 AND id_subscription IN
(SELECT id_subscription FROM invoice
WHERE date_due < CURDATE() AND date_paid IS NULL
GROUP BY id_subscription
HAVING COUNT(*) > 2)

The two queries are not the same. To use the actual query use the NativeQuery createNativeQuery() instead of Query.
In your case the JPA version seems to have syntax errors.
After the AND you are missing the IN operator.
In the nested query you are selecting i instead of something like i.idInvoice
The JPA query should look like
SELECT sub FROM Subscription sub
WHERE sub.isSuspended = 0
AND sub.idSubscription IN
(SELECT i.idInvoice
FROM Invoice i
WHERE i.dateDue < CURRENT_DATE AND i.datePaid IS NULL
GROUP BY i.idInvoice
HAVING COUNT(i.idInvoice) > 2);

Related

how to use date_part in hibernate query?

String q = "select id from Calendar c " +
"where c.isActive = 1 and " +
"date_part('dow', '2017-09-19 13:23:23'::date) = c.frequencyValue)";
Query query = em.createQuery(q);
List results = query.getResultList();
If I include ::date, hibernate would complain because : conflicts with parameter, but if I don't, postgres will complain. Could not choose a best candidate function. You might need to add explicit type casts. What can I do?
https://docs.jboss.org/hibernate/orm/3.3/reference/en/html/queryhql.html#queryhql-expressions
as specified extract function should work if the underlying db supports them so:
extract(dow from date '2017-09-19 13:23:23');
should works

ALL query in Ecto

I need to translate the following SQL to Ecto Query DSL.
SELECT otc.*
FROM users u
INNER JOIN one_time_codes otc ON u.id = otc.user_id
LEFT OUTER JOIN one_time_code_invalidations otci ON otci.one_time_code_id = otc.id
WHERE u.id = 3 AND
otc.code = 482693 AND
otci.inserted_at IS NULL AND
otc.inserted_at > all(SELECT otc.inserted_at
FROM one_time_codes otc2
WHERE otc2.user_id = 3) AND
otc.inserted_at > (now() - '180 seconds'::interval);
In the third AND statement of WHERE clause, notice there is an ALL query. Ecto seems to not have corresponding function in Ecto.Query.API.
How to apply such aggregate? What is the correct way to implement this, though it could be implemented by having a lookup on debug logs of the Ecto, do you have another idea (or suggestion)?
Thank you.
Ecto does not allow subqueries in expressions, and you're right that there's no all in Ecto's Query API, but you can use fragment like this:
where: ...
and otc.inserted_at > fragment("all(SELECT otc.inserted_at FROM one_time_codes otc2 WHERE otc2.user_id = ?", 3)
and ...

Symfony DQL Postgresql group by isnt working

I have just switched from mysql to postgresql and my working dql queries stopped working.
Here is my dql
SELECT p
FROM AppBundle:Photo p
JOIN AppBundle:Like l WITH p = l.photo
WHERE p.isModerated = :isModerated
AND p.isActive = :isActive
AND p.category IN(:categories)
AND p.creationDate <= :begin
AND p.creationDate >= :end
GROUP BY p.id
ORDER BY l.creationDate DESC
Getting the next error
SQLSTATE[42803]: Grouping error: 7 ERROR: column "l1_.creation_date" must appear in the GROUP BY clause or be used in an aggregate function
LINE 1: ... p0_.creation_date >= $4 GROUP BY p0_.id ORDER BY l1_.creati...
As i can understand, it says that group by column should be in SELECT. I dont it to be there. I need to select just p (AppBundle:Photo) and nothing more. What should be edited in dql to get it working properly?
Thank you.
I just replaced ORDER BY l.creationDate DESC to ORDER BY p.creationDate DESC
It will work for me. As i understood, ORDER BY column should be SELECTed

Linq to Entities nested subqueries

I am fairly new to linq and am having a problem translating the following sql to linq:
select
c3.COURSE, c3.DESCR
from
(select c.courseprefix, MAX(c.coursesuffix) [coursesuffix]
from
(select distinct SUBSTRING(course,1,3)[courseprefix], RIGHT(course, LEN(course) - 3) [coursesuffix]
from PsCourses where LEN(course) >= 3 ) c
group by c.courseprefix
) c2 inner join PsCourses c3 on (c2.courseprefix + c2.coursesuffix) = c3.COURSE
where
c3.COURSE_STATUS = 'A'
order by
c3.course
Well, you can use http://www.sqltolinq.com/downloads
or you can make a IQueryable, but in my personal criteria the complex queries I prefer to write in native sql

jpa query for arithmetic operation on columns

I am somewhat new to jpa. I have created a few simple queries. But now i have a problem with a simple query. I have an entity Payment with 3 columns(Id,Amount1,Amount2).
Now i want to find all the rows from Payment where Amount1+Amount2 is greater than somevalue.I tried something like:
Query query = getEntityManager().createQuery(
"Select p from Payment p WHERE p.Amount1 + p.Amount2 >' " + someamount +" ' ");
But it is not working. I tried using SUM also but that is only one column.
Can anyone help me with this.
Thanks.
Assuming your entity is properly defined, the following query should work just fine:
final BigDecimal threshold = BigDecimal.valueOf(1000);
TypedQuery<Payment> query = getEntityManager().createQuery(
"SELECT p FROM Payment p WHERE p.Amount1 + p.Amount2> :value",
Payment.class);
query.setParameter("value", threshold);
List<Payment> results = query.getResultList();