Is it possible to write 'with queries' in jpa specifications - java

I have native query written with 'with queries'. How could it be rewritten by jpa specifications? It's the small part of code
with sub_query1 as (
select table1.id
from table1
join table2 ON table1.id = table2.contract_id
where table2.administrator_id = 11
order by table1.create_date desc
), sub_query2 as (
select table1.id
from table1
join table3 on table1.id = table3.id
where table3.administrator_id = 11
order by table1.create_date desc
)
select table1.id
from table1
where (table1.id in (select id from esa_subquery) or table1.id in (select id from ec_subquery));

Assuming there is a table1 entity, a table2 entity with contract and administrator relations and a table3 with administrator relation, you can rewrite it like that
select t from table1 where exists (select 1 from table2 t2 where t2.contract.id = t.id and t2.administrator.id = 11) or exists (select 1 from table3 t3 where t3.id = t1.id and t3.administrator.id = 11)

Related

JPA select from select distinct

I'm trying to execute the following SQL query writing native query in JPA:
SELECT id FROM (
SELECT DISTINCT id, DENSE_RANK() OVER (ORDER BY id) AS row FROM table
) WHERE row BETWEEN 1 AND 10;
In the Query annotation I wrote:
#Query(value = "SELECT t.id FROM (SELECT DISTINCT(e.id), DENSE_RANK() OVER (ORDER BY e.id) AS row FROM Table e) t WHERE row BETWEEN ? AND ?")
but I have syntax errors here FROM (SELECT.
Please, can you help me?
You have probably forgot nativeQuery=true
Try this:
#Query(value = "SELECT t.id FROM (SELECT DISTINCT(e.id), DENSE_RANK() OVER (ORDER BY e.id) AS row FROM Table e) t WHERE row BETWEEN ? AND ?", nativeQuery = true)

Convert Postgres Query to JPQL

How to convert below PostreSQL query into JPQL? I mainly stuck with row_number(), partition and when case.
select p, c from product p left join
(
select
c.product_id, c.id, c.status, c.start_date,
row_number() over (partition by product_id order by (case status when 'EXPIRED' then 2 else 1 end) asc, start_date desc) as row_number
from contract c
order by product_id
) c
on
p.id = c.product_id and c.row_number = 1
order by p.id, c.start_date;

Can I add amounts from multiple tables in hql

I would like to perform a query in HQL similar to this in SQL:
SELECT (
SELECT COUNT(*) FROM EMPLOYER +
SELECT COUNT(*) FROM COMPANIES
)
FROM DUAL
When I add "FROM DUAL" to the query I get an error:
org.hibernate.hql.ast.QuerySyntaxException: DUAL is not mapped
And if I leave off the "FROM DUAL" I get:
org.hibernate.hql.ast.QuerySyntaxException: unexpected end of subtree
Any recommendations?
In SQL, subqueries need their own set of parentheses, so try this:
SELECT ((SELECT COUNT(*) FROM EMPLOYER) +
(SELECT COUNT(*) FROM COMPANIES)
)
If that doesn't work, resort to a from clause:
SELECT e.cnt + c.cnt
FROM (SELECT COUNT(*) as cnt FROM EMPLOYER
) e CROSS JOIN +
(SELECT COUNT(*) as cnt
FROM COMPANIES
) c
Since you didn't join the Company and Employee tables you'd better run two queries:
int companyCount = ((Number) getSession().createQuery("select count(*) from Company").uniqueResult()).intValue();
int employeeCount = ((Number) getSession().createQuery("select count(*) from Employee").uniqueResult()).intValue();
int totalCount = companyCount + employeeCount;
The (Number) is needed since PostgreSQL returns a BigInteger while HDSQLDB returns an Integer, but both are subclasses of Number.

How to join 2 row_numbers

Im trying to do a query where I want to join the row_numbers RN and RN1 so as to remove the duplicate rows as shown BOLD in the result set.
WITH CTE AS (
SELECT
PrevEndDate = LAG(edate,1) OVER (PARTITION BY id ORDER BY id)
, PrevStartDate = LAG(sdate,1) OVER (PARTITION BY id ORDER BY id)
, p.id, p.edate
, ROW_NUMBER() OVER (PARTITION BY p.id ORDER BY p.id) as RN1
FROM table p
)
SELECT
t.id, t.sdate, t.edate
, (ROW_NUMBER() OVER (PARTITION BY t.id ORDER BY t.id)) AS RN
, CTE.RN1
, CASE
WHEN CTE.PrevEndDate > t.sdate
THEN DATEDIFF(day,CTE.PrevStartDate,t.sdate)
ELSE
DATEDIFF(day,t.sdate,t.edate)
END
FROM table t
INNER JOIN CTE ON CTE.id= t.id AND CTE.edate= t.edate
--AND RN1 = RN
Thanks in Advance!
You can't define an alias in the select and then use it in from (or where for that matter). You can move it into a subquery. Something like:
with cte as (. . .)
SELECT . . .
FROM (SELECT t.*, ROW_NUMBER() OVER (PARTITION BY t.PRTCPNT_DCN ORDER BY t.PRTCPNT_DCN) as RN
FROM PRTCPNT_ELIG_SPAN_T t
) t INNER JOIN
CTE
ON CTE.PRTCPNT_DCN = t.PRTCPNT_DCN AND
CTE.PRTCPNT_ELIG_END_DATE = t.PRTCPNT_ELIG_END_DATE AND
CTE.RN1 = t.RN;

sql : how can i execute two dependents queries?

I have 2 tables (table1, table2) table1 has a field id and table2 has a field id_eid that reference the id field of table1 as foreign key.
I have to delete from table1 all the rows that match a determinated criteria and then if these data are referenced in table2 delete the data from it too.
I do something like that, assuming con is the Connection object and autocommit is set to false on it.
String query1 = "delete from table2 where exists
(select * from table1 where someparameter = ? and table1.id = table2.id_eid)"
then i execute the first query1 using PreparedStatement.
then i have
String query2 = "delete from table1 where someparameter = ?
and exists (select * from table2 where table1.id = table2.id_eid)"
and i executed this with another PreparedStatment.
at the end i have the con.commit().
This doesn't work, i was thinking using autocommit to false the two queries was executed together but it is not, the second query deletes no rows, how can i do this ?
An important note, not all the rows in table1 have a referenced row in table2.
Thanks
You could always query the data to delete first, then delete it second:
1) Select ID from table1 where <criteria>
2) Select ID from table2 where <criteria>
3) Delete from table1 where ID in <results from (1)>
4) Delete from table2 where ID in <results from (2)>
If you
"have to delete from table1 all the rows that match a determinated criteria"
I think String query2 must be
String query2 = "delete from table1 where someparameter = ?"

Categories