Percentage For Data Set

While seeding a database with data for a DataFix Rosetta Code post I thought it would be helpful to be able to determine the percentage (or get as close to it as possible) for a set of data.

Typically this would be in a WHILE loop, for all the examples below the upper bound loop until would have been 1000.

Example use case: for half the data set a date field:

1
2
3
4
IF (@Counter % 2 = 0)
BEGIN
--- run an update to set the date field for 50% of the data
END

Quarters

1
2
3
4
---- QUARTERS
IF (@Counter % 4 > 2) 250
IF (@Counter % 2 = 0) 500
IF (@Counter % 4 < 3) 751

Thirds

1
2
3
---- THIRDS
IF (@Counter % 3 = 0) 333
IF (@Counter % 3 < 2) 667

Tens

1
2
3
4
5
6
7
8
9
10
11
---- TENS
IF (@Counter % 10 = 0) 100
IF (@Counter % 10 < 2) 201
IF (@Counter % 10 < 3) 301
IF (@Counter % 5 < 2) 401
IF (@Counter % 2 = 0) 500
IF (@Counter % 5 < 3) 601
IF (@Counter % 10 < 7) 701
IF (@Counter % 5 < 4) 801
IF (@Counter % 10 < 9) 901
*/

Having a hoon

I used the script below to figure out the values above.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
DECLARE @Counter int = 0;
DECLARE @Hoon int = 0;

WHILE @Counter <= 1000
BEGIN
SET @Counter+=1;

IF (@Counter % 2 = 0)
BEGIN
SET @Hoon+=1;
END
END

PRINT '----------------------'
PRINT(' @Hoon = ' + CONVERT(varchar(1000), @Hoon))
PRINT '----------------------'

References