In my JPA model there are 3 tables A, B, C.
My query is:
SELECT a FROM A a
WHERE EXISTS (
SELECT c from C c LEFT JOIN B b"
ON c = b.c AND b.a = a
WHERE c.date BETWEEN CURRENT_TIMESTAMP AND :pUntil AND b.a IS NULL
)
Background is that I want all entities of A that do not have an entry in b that is linked to an event C in the future.
The problem is that I get Column 'T0.ID' is either not in any table in the FROM list or appears within a join specification and is outside the scope of the join specification or ...
EDIT
: Think of it as A is a user table, C are events, and B stores the registrations of users for events. I want to get all users, which have not registered for all future events until parameter pUntil.
Although I agree with Neil, I worked around this issue by changing my query. Here's the new query:
SELECT a FROM A a
WHERE EXISTS (
SELECT c from C c
WHERE c.date BETWEEN CURRENT_TIMESTAMP AND :pUntil
AND NOT EXISTS (
SELECT b from B b
WHERE b.c= c and b.a = a
)
)
Related
Suppose I have a SQL like that:
SELECT a.*
FROM A a
LEFT JOIN
B b
JOIN C c ON (c.b_id = b.id AND c.whatever > 0)
ON (a.id = b.a_id)
Now I start coding using the criteria API:
CriteriaBuilder builder = em.getCriteriaBuilder();
CriteriaQuery<A> criteria = builder.createQuery(A.class);
Root<A> a = criteria.from(A.class);
Now what?
I can't Join<A, B> b = a.join("b") yet.
The a.join(???) should be called over something else, that happens to have b defined.
That something else is the result of joining together B and C (plus a custom ON clause, stating that only some Cs - those with whatever bigger than zero - are acceptable). That something else still exposes B and C separately (for further predicate filtering with where and/or column projection with select).
UPDATE
What's the difference between
(1) A a LEFT JOIN (B b JOIN C c ON (c.b_id = b.id AND c.whatever > 0)) ON (a.id = b.a_id); and
(2) (A a LEFT JOIN B b ON (a.id = b.a_id)) JOIN C c ON (c.b_id = b.id AND c.whatever > 0)?
[parenthesis were added for clarity; they are never required]
The ON clause can be understood as "closing" the last "still open" JOIN. The JOIN clause (any type) can be understood as "opening" a new JOIN; these JOINs are stacked on "opening" and unstacked on "closing".
In (1), the order is: I-JOIN(B and C), then L-JOIN(A and that).
In (2), the order is: L-JOIN(A and B), then I-JOIN(that and C).
[I-JOIN is an INNER JOIN and L-JOIN is a LEFT OUTER JOIN]
For the sake of simplicity, just take the table C to be empty. So, no matter what JOIN you make, any reference to C (i.e. to its columns) will always be NULL.
Now, in (1) there will be one result per row in A. Because I-JOIN(B and C) will always produce no rows (remember, C is empty, so any INNER JOIN is empty too); then L-JOIN(A and empty) will produce A and all columns from B and C will be NULL.
In (2) the result will be totally empty. No matter how many rows L-JOIN(A and B) produces (there will be at least one row per row of A), the final INNER JOIN with empty C entails an empty result (no rows at all).
I have two table a and b.
I want to update the row in table a that is the most recent insert for each id from the earliest insert in table b where a.id = b.id
I've been trying to use an update statement with a sub select in the from.
If I execute the sub query on its own it returns x number of rows, however when I execute the whole update statement it updated y number of rows.
update a
set title = b.title
created_at = b.created_at
from
(
select
e.id,e.title,e.created_at
from
(
select
l.id,
l.title,
l.created_at
l.t_insert
from b l
left join b r
l.id = r.id and l.t_insert > r.t_insert
) e
join
(
select
l.id,
l.title,
l.created_at,
l.t_insert
from a l
left join a r on l.report_id = r.report_id and l.t_insert <
r.t_insert
) f
)
where
a.id=b.id
I want the same number of rows to be updated as returned in the sub select query in the from.
In this case, having fewer rows updated than returned by the subquery could be because one row id is returned more than once in the subquery. If that happens, the update statement will still only update the row once. I'm assuming the statement you've provided is not exactly what you're running, but you should check that the subquery is not providing duplicates in the id field of the subquery (either using DISTINCT or GROUP BY or by double checking your JOIN conditions.
I have a table x which have the fields a, b, c, and d. I want to do a SELECT statement which is GROUPED BY a HAVING a_particular_value = ANY(array_agg(b)) and retrieves a, MIN(d), and c <- from which row is chosen by a_particular_value = ANY(array_agg(b)).
It's a bit confusing.
Lemme try to explain. a_particular_value = ANY(array_agg(b)) will choose some or one record from all records that is grouped by a. I want to retrieve the value of c from the record that causes the condition to be true. While NOT filter out other records because I still need those for the other aggregate function, MIN(d).
The query that I've tried to make:
SELECT a, MIN(d) FROM x
GROUP BY a
HAVING 1 = ANY(array_agg(b))
The only thing that's left to do is put c in the SELECT clause. How do I do this?
with agg as (
select a, min(d) as d
from x
group by a
having 1 = any(array_agg(b))
)
select distinct on (a, c)
a, c, d
from
x
inner join
agg using (a, d)
order by a, c
If min(d) is not unique within the a group then it is possible to exist more than one corresponding c. The above will return the smallest c. If you want the biggest do in instead
order by a, c desc
c can have various values in this scenario, so your only option is to group by c as well.
SELECT a, c FROM x
GROUP BY a, c
HAVING 1 = ANY(array_agg(b))
If you want to eliminate rows with b not satisfying condition before applying GROUP BY then use WHERE as documentation for HAVING says http://www.postgresql.org/docs/9.2/static/sql-select.html#SQL-HAVING
I encounter a question about crystal report how to group by ?
I have a table named Part
Part_ID Parent_Part_ID
B A
c A
A NULL
C B
D B
E C
F C
A is a top part, B,C,D not only sub part but also are parent part, E,F are lowest sub part.
Now ,I need to show From Parent_part to Part with Levels,Like This
How Can I show this format data in Crystal report
I try use hierachical grouping options ,but the result is not i want
I need the result is:
A
B
C (shold be show even the part have two parent part)
E
F
D
C
E
F
SOLVED!
Tried menu voice called hierachical grouping options and specified the field that links each record to his parent.
The result should look similar to this:
A
B
C
E
F
D
C
E
F
But Crystal Reports Hierarchies take each record just once, and don't care if same element should be under two parents.
So i made a query-hack, creating hierarchy directly in the data using a JOIN on the same table based on Parent Item
DECLARE #t TABLE (Part_ID varchar(1), Parent_Part_ID varchar(1) );
insert into #t
SELECT 'B' , 'A'
UNION SELECT 'C' , 'A'
UNION SELECT 'A' , NULL
UNION SELECT 'C' , 'B'
UNION SELECT 'D' , 'B'
UNION SELECT 'E' , 'C'
UNION SELECT 'F' , 'C'
SELECT
t1.Part_ID as t1,
t2.Part_ID as t2,
t3.Part_ID as t3,
t4.Part_ID as t4
FROM #t t1
LEFT JOIN #t t2 on t1.Part_ID = t2.Parent_Part_ID
LEFT JOIN #t t3 on t2.Part_ID = t3.Parent_Part_ID
LEFT JOIN #t t4 on t3.Part_ID = t4.Parent_Part_ID
WHERE t1.Parent_Part_ID is null
and removed hierarchical grouping options, just created 3 groups on t1, t2, t3 using header for each group and using details for t4.
The result is, as needed,
A
B
C
E
F
D
C
E
F
Here the .rpt if someone needs it.
I have the folowing situation.
I want to retrieve a list of bids between time t1 and time t2. Then from this list i want to retrieve winning bid i.e maximum bid price.
I have written the following JPA query.
SELECT b FROM Bid b WHERE b.bidAmt = (SELECT MAX(b.bidAmt) FROM b WHERE b.lastUpdtTs BETWEEN ?1 AND ?2)
But I am getting the following exception.
Exception Description: Syntax error parsing the query [SELECT b FROM Bid b WHERE b.bidAmt = (SELECT MAX(b.bidAmt) FROM b WHERE b.lastUpdtTs BETWEEN ?1 AND ?2)], line 1, column 64: unexpected token [b].
Internal Exception: NoViableAltException(66!=[1108:1: subselectIdentificationVariableDeclaration[List varDecls] : ( identificationVariableDeclaration[varDecls] | n= associationPathExpression ( AS )? i= IDENT | n= collectionMemberDeclaration );])
Couls someone point out the mistake?
Since you query references two different Bid instances, they should have different aliases:
SELECT b FROM Bid b WHERE b.bidAmt =
(SELECT MAX(bb.bidAmt) FROM Bid bb WHERE bb.lastUpdtTs BETWEEN ?1 AND ?2)
Haven't tried it myself, but from what I see, mistake could be in your subquery. You say FROM b, but it should be FROM Bid b. So, the entire query looks like this:
SELECT b FROM Bid b WHERE b.bidAmt = (SELECT MAX(b.bidAmt) FROM Bid b WHERE b.lastUpdtTs BETWEEN ?1 AND ?2)
The Error you got the second time is
multiple declaration of identification
variable [b], previously declared as
[Bid b]
From the above error it seems like b alias declared for multiple times so remove alias b from sub query
try out following query
SELECT b FROM Bid b WHERE b.bidAmt =
(SELECT MAX(bidAmt) FROM Bid WHERE lastUpdtTs BETWEEN ?1 AND ?2)