June 30, 2018 at 3:59 am
Comments posted to this topic are about the item Generating Completely Random Sample Output from a Dataset
July 12, 2018 at 5:31 am
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.
July 12, 2018 at 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.
July 13, 2018 at 9:26 pm
Chuck Bevitt - Thursday, July 12, 2018 12:03 PMInteresting, 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 MessagesWhere '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
Change is inevitable... Change for the better is not.
July 16, 2018 at 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 ...
July 16, 2018 at 8:22 am
julian.fletcher - Monday, July 16, 2018 4:30 AMInteresting, 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
Change is inevitable... Change for the better is not.
July 16, 2018 at 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.
July 16, 2018 at 2:19 pm
julian.fletcher - Monday, July 16, 2018 8:43 AMThe "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! š
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 8 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply