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)
 UNION and TOP

Author  Topic 

ckuo@kahluadesigns.com
Yak Posting Veteran

58 Posts

Posted - 2003-11-24 : 17:00:39
Hi,
Say I have

SELECT ProductID, Price FROM Table1 WHERE CategoryID = 10
UNION
SELECT ProductID, Price FROM Table2 WHERE CategoryID = 10

How do I specify a TOP 20 of both statments or after the union. Problem I am having is that when I sort this by Price, I get results of rows before the union. I just want to sort within these two statements. Thanks.

chadmat
The Chadinator

1974 Posts

Posted - 2003-11-24 : 17:41:11
select top 20 ...
FROM (
SELECT ProductID, Price FROM Table1 WHERE CategoryID = 10
UNION
SELECT ProductID, Price FROM Table2 WHERE CategoryID = 10
) a
order by Price asc


-Chad

http://www.clrsoft.com

Software built for the Common Language Runtime.
Go to Top of Page

Granick
Starting Member

46 Posts

Posted - 2003-11-24 : 18:13:45
It is really funny. I was going to suggest this exact idea, but when I did a quick test on some of my own data tables, I got an error, so I didn't. I went ahead and set up trial similar to what is happening here, it worked.

Very weird.

Shannon
Go to Top of Page

mohdowais
Sheikh of Yak Knowledge

1456 Posts

Posted - 2003-11-25 : 01:58:40
>>It is really funny. I was going to suggest this exact idea, but when I did a quick test on some of my own data tables, I got an error, so I didn't. I went ahead and set up trial similar to what is happening here, it worked.

I bet you forgot the alias for the subquery.

Owais


Please excuse my driving, I am reloading.
Go to Top of Page
   

- Advertisement -