I have a scenario where I need to define multiple predicates and return a specification. The operations I need to perform are
1. A join
2. In clause
3. Group by clause
4. Count(*) >= some_number
Below is the query that I need to convert as a specification.
SELECT * FROM tableA A join tableB B
on B.some_id_1 = A.some_id_1
where B.some_id_2 = 18 and A.some_id_3
in (1,2,3)
group by A.some_id_1
having count(*) >= 3
How do I build this specification using Spring Data JPA
Related
normal sql query which work correctly in db in sql developer passing values for bID and period.
SELECT * FROM A WHERE abcID IN (SELECT abcID FROM B WHERE bID=1) AND period=3
in project at Repository class I passed as this
#Query("select a from A where a.abcID IN:(select b.abcId from B where bID=:RevID) and period=:period")
error comes as
Space is not allowed after parameter prefix ':' [select a from A
where a.abcID IN:(select b.abcId from B where bID=:RevID) and
period=:period]
I want to know how should I insert above query correctly in #Query annotation
First of all I would tell you below points.
you can't use select query as select a from A where a.abcID. Here a is a column so can't define something like a.abcID. It need to be select column from tableA a where a.abcID
Same for query use in IN clause. It needs to be like select b.abcId from tableB b where b.bID=:RevID
What you use as :RevID, :period need to be passed as #Param("RevID"), #Param("period") to the query method.
This is the query template.
#Query("select a.nameOfcolumnYouWantToRetrieve from tableA a where a.someColumn in(select b.someColumn from tableB b where b.columnValueOfTableBYouwantToMatch=:RevID) and a.period=:period")
Using this points, try below query.
#Query("select a.id from A a where a.abcID in(select b.abcId from B b where b.bID =:RevID) and a.period=:period")
is there any better way to write this query to be optimized?
SELECT * FROM data d
WHERE d.id IN (SELECT max(d1.id)
FROM data d1
WHERE d1.name='A'
AND d1.version='2')
I am not so good with SQL.
With PostgreSQL v13, you can do it like this:
SELECT * FROM data
WHERE name = 'A'
AND version = '2'
ORDER BY is DESC
FETCH FIRST 1 ROWS WITH TIES;
That will give you all rows where id is the maximum.
If id is unique, you can use FETCH FIRST 1 ROWS ONLY or LIMIT 1, which will also work with older PostgreSQL versions.
Apart from other answers that are equally interesting / correct, IN is typically a non-performant keyword. You can remove it by using a slightly different way of writing your own query:
SELECT * FROM data d
WHERE d.name = 'A' and d.version = '2' and
d.id = (SELECT max(d1.id) FROM data d1 WHERE d1.name='A' AND d1.version='2')
I have input from a tableA in database A that I would like to join to another tableB in database B.
These were my two options:
Use Database Join: For each input from table in database A, run the
join query in database B.
Use two Input tables (talbeA + tableB) and do merge join on key.
I went with option #1 as I want to avoid reading in tableA and tableB in entirety.
My question is:
How can I use all results from a prior step as one "IN" query?
For instance
select *
from tableB b
where b.id IN (all_rows_from_prior_step)
versus (where it runs for each input row)
select *
from tableB b
where b.id = ?
Use 'Group by' to flatten the rows into one row with a field 'all_rows_from_prior_step' of comma separated ids (Group field: empty, Name: all_rows_from_prior_step, Subject: id, Type: 'Concatenate strings separated by ,'). Next, use a 'User Defined Java Expression' to build the sql query:
"select * from tableB b where b.id IN (" + all_rows_from_prior_step + ")"
Last, use 'Dynamic SQL row' to run the query. The template sql could be
select * from tableB b where 1=0
Is thare any way a JQL can get the counts according to the values of an Enum? For example, Enum has values as Single, Married, Divorced, etc. and get the counts of Persons Entity in a single Java Persistence Language Query?
There are two ways, either use a group by, or sub-selects in the Select clause,
Select Count(p), p.status from Person p group by p.status
or,
Select (Select Count(p) from Person p where p.status = Status.Single), (Select Count(p) from Person p where p.status = Status.Married), (Select Count(p) from Person p where p.status = Status.Divorced)
but sub-selects in the Select clause is not supported by JPA (at least JPA 2.0, 2.1 does I believe), EclipseLink 2.4 or greater does support this.
I'm using Db2 on AS/400, and I am trying to execute a JPQL query that will return results from row x to row y.
In SQL this works:
select cur.* from (
SELECT ROW_NUMBER() OVER() AS ROWNUM FROM tableName d) as cur
WHERE cur.ROWNUM > 0 AND cur.ROWNUM < 10
How can I do this in JQPL? I tried it in many ways but every time I got an exception.
I want to limit my result inside the query, and not by using the setMaxResult, setFirstResult methods.
Query q = em.createQuery("select e from SomeEntity e")
.setFirstResult(0)
.setMaxResults(10);
That cannot be done. JPQL operates to entities and entities are mapped to tables in database. Row number in db2 is concept in result set, not in database table.