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)
 Random rows in Union statement

Author  Topic 

Yula
Starting Member

4 Posts

Posted - 2007-05-09 : 02:38:48
Can anybody help me with this?

i want to get random rows coming from this query.

select columnID, column1
from table1
union
select columnID, column2
from table2

I was wondering if i can get for example 5 random rows from this query.

Hope somebody can help.. thanks in advance..

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-05-09 : 02:46:50
select top 5 columnid, col
from (
select columnID, column1 as colfrom table1
union
select columnID, column2 from table2
) as x
order by newid()


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

Yula
Starting Member

4 Posts

Posted - 2007-05-09 : 03:26:00
quote:
Originally posted by Yula

Can anybody help me with this?

i want to get random rows coming from this query.

select columnID, column1
from table1
union
select columnID, column2
from table2

I was wondering if i can get for example 5 random rows from this query.

Hope somebody can help.. thanks in advance..



let me change the problem..

I want to get random columnIDs from the query..
Go to Top of Page

pbguy
Constraint Violating Yak Guru

319 Posts

Posted - 2007-05-09 : 04:12:56
Did you check peter's query ?
put some sample data and required o/p.
Go to Top of Page
   

- Advertisement -