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 with NEWID()

Author  Topic 

dmgotty
Starting Member

4 Posts

Posted - 2012-02-02 : 17:08:55
Hello there,

I was wondering if anyone knows the solution to the following;

I want to union the same table on itself whilst returning a random result based on a with statement... Im sure the following code (which doesnt work) will explain what im attempting much easier.

SELECT TOP 1 *
FROM tableName
WHERE field = x
ORDER BY newid()
UNION ALL
SELECT TOP 1 *
From tableName
WHERE field = y
ORDER BY newid()
.. etc

russell
Pyro-ma-ni-yak

5072 Posts

Posted - 2012-02-02 : 17:20:44
[code]SELECT * FROM (
SELECT TOP 1 *
FROM tableName
WHERE field = x
ORDER BY newid()
) a
UNION ALL
SELECT * FROM (
SELECT TOP 1 *
From tableName
WHERE field = y
ORDER BY newid()
) b
[/code]
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2012-02-03 : 03:42:33
Yesterday I really thought about "where is russell?" and voilá there is a post - that's fine



No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

russell
Pyro-ma-ni-yak

5072 Posts

Posted - 2012-02-03 : 16:48:59
Yeah, sorry for not posting lately. A few business things, and a few family things have kept me pretty busy lately. But all is well and I should be around much more now
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2012-02-03 : 16:52:04
very glad to hear that all is well.


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page
   

- Advertisement -