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 2005 Forums
 Other SQL Server Topics (2005)
 select Top

Author  Topic 

wided
Posting Yak Master

218 Posts

Posted - 2010-06-04 : 11:51:06
Hello

i have to select records with top(n), but i must use UNION

select top(10) col1, col2
from table1

UNION
select top(10) col1, col2
from table2

I want tp have only 10 records from table1 and table2

how ca, i write it, this query return 10 from table1 et 10 from table2

Thanks

vijayisonly
Master Smack Fu Yak Hacker

1836 Posts

Posted - 2010-06-04 : 11:53:05
[code]select top(10) col1, col2
from
(
select col1, col2
from table1
UNION
select col1, col2
from table2
)
order by col[/code]
Go to Top of Page

wided
Posting Yak Master

218 Posts

Posted - 2010-06-04 : 12:58:37
THANKS VIJAYISONLY

i try it but i have an error near order by
Go to Top of Page

vijayisonly
Master Smack Fu Yak Hacker

1836 Posts

Posted - 2010-06-04 : 13:29:40
Whats the error? Did you replace it with your proper column name?

edit: Oops ..maybe because I dint specify the alias.
Try
select top(10) col1, col2
from
(
select col1, col2
from table1
UNION
select col1, col2
from table2
) t
order by col
Go to Top of Page
   

- Advertisement -