SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Generating Completely Random Sample Output from a Dataset


Generating Completely Random Sample Output from a Dataset

Author
Message
Adam Aspin
Adam Aspin
Hall of Fame
Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)

Group: General Forum Members
Points: 3766 Visits: 1059
Comments posted to this topic are about the item Generating Completely Random Sample Output from a Dataset
nir-616899
nir-616899
Forum Newbie
Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)

Group: General Forum Members
Points: 8 Visits: 93
Hi,
Thank you for the quick tip.


I believe a follow up is in need.

In case the manager asks for a new batch of random customers after a while,
The "on the fly" GUID(NewID), although it is generated uniquely per run,
might produce some of the customers that were on the previous run.

Therefore I’d suggest to add a BIT type column (0,1) to monitor which customers were called,

so you could mark them up and exclude them from your next runs.

SELECT TOP 5 PERCENT *
FROM Data.SalesByCountry
WHERE Called = 0 -- 0 = not called, 1 = called
ORDER BY NEWID()

Make sure you update the [Called] column to '1'
for the customers you called.

A select with output/update query will do it at one go.

Chuck Bevitt
Chuck Bevitt
Mr or Mrs. 500
Mr or Mrs. 500 (508 reputation)Mr or Mrs. 500 (508 reputation)Mr or Mrs. 500 (508 reputation)Mr or Mrs. 500 (508 reputation)Mr or Mrs. 500 (508 reputation)Mr or Mrs. 500 (508 reputation)Mr or Mrs. 500 (508 reputation)Mr or Mrs. 500 (508 reputation)

Group: General Forum Members
Points: 508 Visits: 113
Interesting, because I had thought that any function that took no parameters or a hard coded value parameter would only be evaluated once and would return the same value for every record in the returning record set. Examples of this are Rand() and GetDate(). So it appears that NewID() is an exception. I just had to test this:

CREATE TABLE dbo.TestFunctions(
PrimaryKey int IDENTITY(1,1) NOT NULL,
UniqueID uniqueidentifier NOT NULL,
Random float NOT NULL,
CONSTRAINT PK_TestFunctions PRIMARY KEY CLUSTERED (PrimaryKey ASC))

And then:

Insert into dbo.TestFunctions (UniqueID, Random)
Select top 10
NewID(),
Rand()
From Messages

Where 'Messages' is any table with at least 10 records. Sure enough, UniqueID gets different values while Random gets the same value in every record.

The fact that Rand() returns the same value for every record has been a source of frustration and innumerable forum threads over the years. As the article illustrates, using NewID() instead of Rand() would solve the problem posted in these forum threads.
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (817K reputation)SSC Guru (817K reputation)SSC Guru (817K reputation)SSC Guru (817K reputation)SSC Guru (817K reputation)SSC Guru (817K reputation)SSC Guru (817K reputation)SSC Guru (817K reputation)

Group: General Forum Members
Points: 817921 Visits: 46323
Chuck Bevitt - Thursday, July 12, 2018 12:03 PM
Interesting, because I had thought that any function that took no parameters or a hard coded value parameter would only be evaluated once and would return the same value for every record in the returning record set. Examples of this are Rand() and GetDate(). So it appears that NewID() is an exception. I just had to test this:

CREATE TABLE dbo.TestFunctions(
PrimaryKey int IDENTITY(1,1) NOT NULL,
UniqueID uniqueidentifier NOT NULL,
Random float NOT NULL,
CONSTRAINT PK_TestFunctions PRIMARY KEY CLUSTERED (PrimaryKey ASC))

And then:

Insert into dbo.TestFunctions (UniqueID, Random)
Select top 10
NewID(),
Rand()
From Messages

Where 'Messages' is any table with at least 10 records. Sure enough, UniqueID gets different values while Random gets the same value in every record.

The fact that Rand() returns the same value for every record has been a source of frustration and innumerable forum threads over the years. As the article illustrates, using NewID() instead of Rand() would solve the problem posted in these forum threads.


Then you'll be interested in the following articles...
http://www.sqlservercentral.com/articles/Data+Generation/87901/
http://www.sqlservercentral.com/articles/Test+Data/88964/

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Julian Fletcher
Julian Fletcher
SSCommitted
SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)

Group: General Forum Members
Points: 1993 Visits: 1180


Interesting, because I had thought that any function that took no parameters or a hard coded value parameter would only be evaluated once and would return the same value for every record in the returning record set. Examples of this are Rand() and GetDate(). So it appears that NewID() is an exception.

The fact that Rand() returns the same value for every record has been a source of frustration and innumerable forum threads over the years.


Absolutely!

Also note that, while this approach is great in that you don't need to make any assumptions about the table's schema, it's pretty bad from a performance point of view - I think the whole table gets loaded in to memory. That might not be a problem in many cases, but if your table is large ...


Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (817K reputation)SSC Guru (817K reputation)SSC Guru (817K reputation)SSC Guru (817K reputation)SSC Guru (817K reputation)SSC Guru (817K reputation)SSC Guru (817K reputation)SSC Guru (817K reputation)

Group: General Forum Members
Points: 817921 Visits: 46323
julian.fletcher - Monday, July 16, 2018 4:30 AM


Interesting, because I had thought that any function that took no parameters or a hard coded value parameter would only be evaluated once and would return the same value for every record in the returning record set. Examples of this are Rand() and GetDate(). So it appears that NewID() is an exception.

The fact that Rand() returns the same value for every record has been a source of frustration and innumerable forum threads over the years.


Absolutely!

Also note that, while this approach is great in that you don't need to make any assumptions about the table's schema, it's pretty bad from a performance point of view - I think the whole table gets loaded in to memory. That might not be a problem in many cases, but if your table is large ...



If you build a whole table then, yes, the whole table will be in memory if it actually fits. SQL Server cannot create a table without going through memory.

Shifting gears a bit, you say "it's pretty bad from a performance point of view". I've lost your train of thought. What is "it" in this case?

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Julian Fletcher
Julian Fletcher
SSCommitted
SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)

Group: General Forum Members
Points: 1993 Visits: 1180
The "it" was the suggested solution:
SELECT       TOP 5 PERCENT *
FROM Data.SalesByCountry
ORDER BY NEWID()

The table is already there - the task was to retrieve some records randomly.

Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (817K reputation)SSC Guru (817K reputation)SSC Guru (817K reputation)SSC Guru (817K reputation)SSC Guru (817K reputation)SSC Guru (817K reputation)SSC Guru (817K reputation)SSC Guru (817K reputation)

Group: General Forum Members
Points: 817921 Visits: 46323
julian.fletcher - Monday, July 16, 2018 8:43 AM
The "it" was the suggested solution:
SELECT       TOP 5 PERCENT *
FROM Data.SalesByCountry
ORDER BY NEWID()

The table is already there - the task was to retrieve some records randomly.


In that case, I totally agree! BigGrin

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum








































































































































































SQLServerCentral


Search