-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Is there a limit to nested queries against the same table?
PostPosted: Tue Dec 28, 2004 6:52 pm 
Newbie

Joined: Tue Dec 28, 2004 6:01 pm
Posts: 2
Hibernate version: 2.1.1 and 2.1.7c

Name and version of the database you are using: Postgresql 7.4.1

Is there a limit to how many levels you can nest a subquery in HQL? I have this HQL query:
Code:
  FROM EncounterCard AS enc
  WHERE enc.patientId = ?
  AND enc.encId =
      (SELECT MAX(sub3.encId)
         FROM EncounterCard AS sub3
         WHERE sub3.masterId = enc.masterId
         AND sub3.messageSequence =
             (SELECT MAX(sub2.messageSequence)
                FROM EncounterCard AS sub2
                WHERE sub2.masterId = enc.masterId
                AND sub2.messageTimeStamp =
                    (SELECT MAX(sub1.messageTimeStamp)
                      FROM EncounterCard AS sub1
                      WHERE sub1.masterId = enc.masterId)))
  ORDER BY enc.encounterTimeStamp DESC

When I pass it a parameter of 'pid-1-1', according to QueryTranslator's debug the generated SQL is:
Code:
  select encounterc0_.encid as encid,
         encounterc0_.patientid as patientid,
         encounterc0_.encdate as encdate, encounterc0_.masterid as masterid,
         encounterc0_.msgtimestamp as msgtime12_,
         encounterc0_.msgsequence as msgsequ13_
    from encounters encounterc0_
    where (encounterc0_.patientid='pid-1-1' )
    AND(encounterc0_.encid=
      (select MAX(encounterc1_.encid)
         from encounters encounterc1_
         where (encounterc1_.masterid=encounterc0_.masterid )
         AND(encounterc1_.msgsequence=
           (select MAX(encounterc0_.msgsequence)
              from encounters encounterc0_
              where (encounterc0_.masterid=encounterc0_.masterid )
              AND(encounterc0_.msgtimestamp=
                (select MAX(encounterc0_.msgtimestamp)
                   from encounters encounterc0_
                   where (encounterc0_.masterid=encounterc0_.masterid )))))))
  order by  encounterc0_.encdate DESC;

Notice how after the first subquery 'encounterc0_' is reused instead of 'encounterc2_' or 'encounterc3_'. Is there a nesting level setting that I'm missing?

If it matters, the types for the above fields are:
Code:
  encId              : long
  patientId          : string
  masterId           : string
  messageSequence    : long
  messageTimeStamp   : timestamp
  encounterTimeStamp : timestamp

Thanks for any leads.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.