SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Union with NEWID()
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

dmgotty
Starting Member

4 Posts

Posted - 02/02/2012 :  17:08:55  Show Profile  Reply with Quote
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

USA
5072 Posts

Posted - 02/02/2012 :  17:20:44  Show Profile  Visit russell's Homepage  Reply with Quote
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

Edited by - russell on 02/02/2012 17:22:34
Go to Top of Page

webfred
Flowing Fount of Yak Knowledge

Germany
8766 Posts

Posted - 02/03/2012 :  03:42:33  Show Profile  Visit webfred's Homepage  Reply with Quote
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

USA
5072 Posts

Posted - 02/03/2012 :  16:48:59  Show Profile  Visit russell's Homepage  Reply with Quote
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
Flowing Fount of Yak Knowledge

Germany
8766 Posts

Posted - 02/03/2012 :  16:52:04  Show Profile  Visit webfred's Homepage  Reply with Quote
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
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.17 seconds. Powered By: Snitz Forums 2000