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.
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 tableNameWHERE field = xORDER BY newid()UNION ALLSELECT TOP 1 *From tableNameWHERE field = yORDER 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()) aUNION ALLSELECT * FROM ( SELECT TOP 1 * From tableName WHERE field = y ORDER BY newid()) b[/code] |
|
|
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. |
|
|
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 |
|
|
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. |
|
|
|
|
|