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

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 TOP 5 OF UNION QUERY?
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

ywb
Yak Posting Veteran

55 Posts

Posted - 12/19/2005 :  16:50:37  Show Profile  Reply with Quote
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

Sweden
3279 Posts

Posted - 12/19/2005 :  16:59:14  Show Profile  Reply with Quote
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 - 12/19/2005 :  17:16:04  Show Profile  Reply with Quote
Thanks!
Go to Top of Page
  Previous Topic Topic Next Topic  
 Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.1 seconds. Powered By: Snitz Forums 2000