Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 TOP 5 OF UNION QUERY?

Author  Topic 

ywb
Yak Posting Veteran

55 Posts

Posted - 2005-12-19 : 16:50:37
Hi,

How do I take only the top 5 entries of a UNION statement?

For example, I have a query

SELECT * FROM userTable WHERE (condition 1)
UNION
SELECT * FROM userTable WHERE (condition 2)
UNION
SELECT * FROM userTable WHERE (condition 3)
ORDER BY username

And I only want only the top 5 of this. How can I do it?

Thanks,
W.

rockmoose
SQL Natt Alfen

3279 Posts

Posted - 2005-12-19 : 16:59:14
select top 5 from userTable
where (condition 1) or (condition 2) or (condition 3)
order by uername
-----------------------------------------------------
select top 5 * from
(SELECT * FROM userTable WHERE (condition 1)
UNION
SELECT * FROM userTable WHERE (condition 2)
UNION
SELECT * FROM userTable WHERE (condition 3)
ORDER BY username) as unionedResult

Why We Be?
____________________________________
knowledge comes with death's release
rockmoose
Go to Top of Page

ywb
Yak Posting Veteran

55 Posts

Posted - 2005-12-19 : 17:16:04
Thanks!
Go to Top of Page
   

- Advertisement -