How to convert sql query mysql using criteriaquery - spring

How to convert sql query mysql using criteriaquery this code:
SELECT a.customer as customer, a.cbgroup, a.cbdept, a.buc, a.customer_group, a.product, a.valuta, a.a_amount, a.cbgroup, b.b_amount, (b.b_amount-a.a_amount) as percentage
FROM (
SELECT a.customer, a.date, a.cbgroup, a.cbdept, a.buc, a.customer_group, a.product, a.valuta, SUM(a.amount) as a_amount FROM summaryfee as a WHERE a.date = '2019-06-30' AND a.cbgroup = 'CORPORATE BANKING 1' GROUP BY a.customer
) AS a INNER JOIN
(
SELECT b.customer, SUM(b.amount) as b_amount FROM summaryfee as b WHERE b.date = '2020-06-30' AND b.cbgroup = 'CORPORATE BANKING 1' GROUP BY b.customer
) AS b
ON a.customer=b.customer AND (b.b_amount-a.a_amount) < 0 ORDER BY CASE WHEN a.customer NOT LIKE '%Transaksi Nasabah%' THEN 0
ELSE 1
END, percentage asc LIMIT 5

You can rewrite a JPQL query as a criteria query, but not any native SQL query.
The problem at a glance with your query above is that uses subqueries in FROM clause.
JPQL query language and criteria query at least up to 2.1 JPA spec, support subqueries only in WHERE and HAVING clauses.

Related

JPQL query with having oracle db

I am using jpql jpa eclipselink Following query wont work :
SELECT c FROM TableA c WHERE c.forumId = :forumId AND c.isDefault = true HAVING MAX (c.validFrom)
The error im getting "The expression is not a valid conditional expression"
The HAVING clause only works with a GROUP BY expression.
The HAVING clause allows for the results of a GROUP BY to be filtered.
Your question is:
i want o get max validFrom how can i make expression ot of this
But you can make a query without GROUP BY and HAVING to do what you want:
select c from TableA c WHERE c.validFrom = (
SELECT MAX(c2.validFrom)
FROM TableA c2
WHERE c2.Id = c.Id AND c.forumId = :forumId AND c.isDefault = true
)
If you would like to use GROUP BY and HAVING, you can do:
SELECT c FROM TableA c WHERE c.validFROM = (
SELECT MAX(validFROM)
FROM TableA
WHERE forumId = :forumId AND isDefault = true
GROUP BY validFROM
HAVING validFROM=c.validFROM
)

Ecto Model - subquery in select

I need to make this SQL query with Ecto:
SELECT users.*, (select count(0) from money_transactions where from_id = users.id AND created_at > '2016-1-25 0:00:00.000000') as money_transactions_today_db FROM "users" WHERE "users"."client_token" = '123'
I try to do something like this but it doesn't work:
query = from users in Like4uElixir.User,
where: users.client_token in ^tokens,
select: {users, (from money_transactions in Like4uElixir.MoneyTransaction,
where: money_transactions.from_id == users.id,
select: count(0))}
Does Ecto support subqueries? If not, how can I execute the query?
You can use query fragments:
query = from users in Like4uElixir.User,
where: users.client_token in ^tokens,
select: {users, (fragment("(SELECT COUNT(0) FROM money_transactions
WHERE money_transactions.from_id == ?)", users.id))}
Although in this case the query can also be written using regular joins and group_by. Current support for subqueries in Ecto is limited.

how to write this oracle query in jpa?

Query:
select *
from easquestionsinfo
where questionname in(select questionname
from easresponseinfo
where isconflict = 'yes')
This query works fine and returns me the records from table 'easquestioninfo' when questionname is equal to the one returned by the inner query which returns set of questionname where isconflict='yes'.
JPA supports JPQL, SQL, and Criteria.
You can execute this SQL directly using createNativeQuery().
For JPQL, it depends on your object model, perhaps something like,
Select q fom QuestionInfo q where q.name in (Select r.name from ResponseInfo q2 where r.isConflict = 'yes')
See,
http://en.wikibooks.org/wiki/Java_Persistence/JPQL

Convert oracle query to HQL by using subquery

I'm really confusing about sub Query of hibernate.
I've standard oracle query but unable to convert it into HQL.
select distinct b.nameId
from
(
select nameId from seg_user where id=1
)a, seg_user b
where b.id=a.nameId
can somebody convert it to HQL by using SubQuery or Crieteria
select distinct b.nameId
from seg_user b
where b.id = some (
select a.nameId from seg_user a where a.id=1
)
You can see how to use subqueries here: http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/queryhql.html#queryhql-subqueries

nested subquery in linq to entities

Hi i want to know how to create nested subquery in linq to entities.
e.g. this is my sql subquery which i want to translate in linq.
SELECT
#planned = COUNT(ID)
FROM Task_Detail
WHERE task_id IN
(
SELECT
task_id
FROM Story_Task
WHERE is_testcase = 'true' AND
story_id IN
(
SELECT str_id FROM dbo.story
WHERE prjId = #pro_id AND
str_id IN
(
SELECT str_id FROM dbo.Sprint_StoryMapping WHERE sprint_id = #sprintid
) AND is_testcase = 'true'
)
) AND status = 1 AND
DATEPART(dd,workDate) = DATEPART(dd,#stdt)
Gut feel is that if you just turn this into a single select with the tables joined together, the whole query is going to run a lot faster and be easier to work with. Especially since your after an aggregate result.

Resources