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
 Transact-SQL (2005)
 Question about ORDER BY NEWID()

Author  Topic 

nanarose
Starting Member

2 Posts

Posted - 2011-03-09 : 12:27:29
Hi everyone, I have a question about sentence NEWID() in T-SQL, specifically using it's used with ORDER BY.

I need to generate a random sample, and I know I can use ORDER BY NEWID() to accomplish that. Is that correct?

Also, I'd like to prove that the random sample generated through this sentence belongs to a uniform distribution.

Hope you can help me, many thanks.

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-03-09 : 12:30:07
Yes that's correct, it will produce a random sample.

But uniform distribution? Can you show us an example?

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

nanarose
Starting Member

2 Posts

Posted - 2011-03-09 : 12:58:14
Hi, thanks for the response.

With uniform distribution I mean: if the ORDER BY NEWID() sentence (used with the TOP n) produces intervals such that every item in each interval has the same probability to belong to the sample.

Actually, all I know is that NEWID() assigns a GUID to each row, depending on the IP address and system time, but I don't know what happens next when you add the ORDER and finally the TOP n. Technically, how does it generates the sample?

SELECT TOP n FROM table ORDER BY NEWID()


Thank you all!

Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-03-09 : 13:03:50
I don't know about the technical details, but it will generate a different value each time it is run. It is completely random.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -