0

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?

3 Answers 3

1

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) 
Sign up to request clarification or add additional context in comments.

Comments

1

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)

1 Comment

i tried the above but got the following exception Exception Description: Error compiling the query [SELECT b FROM Bid b WHERE b.bidAmt = (SELECT MAX(b.bidAmt) FROM Bid b WHERE b.lastUpdtTs BETWEEN ?1 AND ?2)], line 1, column 68: multiple declaration of identification variable [b], previously declared as [Bid b]
0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.