Prob w/ RANDOM in a query (1 Viewer)

BennyLinton

Registered User.
Local time
Yesterday, 16:36
Joined
Feb 21, 2014
Messages
263
I am using the SQL below to successfully return a set of 10% of my total eligible, however I need to change this to 2% but it returns NO records... puzzled:

INSERT INTO Random_Temp ( indx, peopleId, audited )
SELECT TOP 10 PERCENT b.indx, b.peopleId, -1 AS audited
FROM dbo_Billing AS b
ORDER BY Rnd(-(1000*b.indx)*Time());
 

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 16:36
Joined
Oct 29, 2018
Messages
21,453
How did you change it? Did you simply change 10 PERCENT to 2 PERCENT and left everything else the same?
 

BennyLinton

Registered User.
Local time
Yesterday, 16:36
Joined
Feb 21, 2014
Messages
263
Yes just changed the 10 to a 2.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 07:36
Joined
May 7, 2009
Messages
19,231
that only means that your record count is below 50.

2% of 50 records = 1 record!
 

BennyLinton

Registered User.
Local time
Yesterday, 16:36
Joined
Feb 21, 2014
Messages
263
My record count is 70 which with the original code returned 7 records.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 07:36
Joined
May 7, 2009
Messages
19,231
it should at least retrieve 1 record?

2% of 70 records = 1.4 records!

so why not use Top 1 or Top 2.
 

BennyLinton

Registered User.
Local time
Yesterday, 16:36
Joined
Feb 21, 2014
Messages
263
Because the record set will vary and it needs to automatically return 2%.
 

BennyLinton

Registered User.
Local time
Yesterday, 16:36
Joined
Feb 21, 2014
Messages
263
Should I have altered this line also?:

ORDER BY Rnd(-(1000*b.indx)*Time());
 

MajP

You've got your good things, and you've got mine.
Local time
Yesterday, 19:36
Joined
May 21, 2018
Messages
8,525
The index is needed to get a unique value per row and the time is good to get a unique sequence. However the - 1000 does nothing but does not hurt
 

Users who are viewing this thread

Top Bottom