Gurus,<br><br>I have following SQL statement:<br><br>SELECT A, B, C<br>FROM T, S<br>WHERE ...<br>UNION<br>SELECT A, B, C<br>FROM T, P<br>WHERE ...<br><br>and I just need the first 50 rows as result.<br><br>How can I do this?<br><br>TIA<br>Mack
SELECT * FROM<br>(SELECT A, B, C<br>FROM T, S<br>WHERE ...<br>UNION<br>SELECT A, B, C<br>FROM T, P<br>WHERE ...)<br>WHERE rownum<=50; but you don't know which 50 rows will be retrieved. You can refine your query an other way.<br> <p>Eduard Stoleru<br><a href=mailto:e_stoleru@yahoo.com>e_stoleru@yahoo.com</a><br><a href= > </a><br>
My answer was based on Oracle. I don't deny that, in MS SQL, TOP command works (perhaps) faster. But, you see, the three little dots you've used replace the long subquery needed for combining the two select's. So, how do you define <b>easy</b>? <p>Eduard Stoleru<br><a href=mailto:e_stoleru@yahoo.com>e_stoleru@yahoo.com</a><br><a href= > </a><br>
Thanks for your replies.<br><br>I think stoleru's solution works fine for me. It doesn't matter which 50 rows from the<br>two SELECT queries will be retrieved. I can add an ORDER BY:<br><br>SELECT A, B, C FROM<br> (SELECT A, B, C<br> FROM T, S<br> WHERE ...<br> UNION<br> SELECT A, B, C<br> FROM T, P<br> WHERE ...)<br> WHERE rownum<=50 ORDER BY A<br><br>Note that the statement used in my case is more complicated than the one given<br>here, I have to write all attributes (A, B, C) instead of a single *, otherwise I get<br>an error:<br><br>Error: ORA-00918: column ambiguously defined<br><br>Mack
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.