Wednesday, December 2, 2026

Easiest way to select records randomly

I have written about using the Random scalar function to generate random numbers before. This is a method I stumbled into when selecting a fixed number of randomly selected rows from a table using the Random scalar function in the ORDER BY clause.

In this scenario I want to select a records from my Person file in a random order. I am only interested in two columns:

  1. Person identifier
  2. Person's name

If I wanted to take quick look at those values for all the rows in the file, I would use the following:

01  SELECT PID, FIRST_NAME || ' ' || LAST_NAME
02    FROM PERSON

Line 1: PID is the person identifier. I am formatting the person's name by concatenating the first and last names columns together to make a new column. The double pipe ( || ) is a shortcut for the concatenate.

I am not going to show all the results from this statement as there are just too many. Below are the first five rows of the results:

PID    00002
----   ---------------
   1   REG ALLEN
   2   JACK CROMPTON
   3   ROGER BYRNE
   4   JOHNNY CAREY
   5   THOMAS MCNULTY

The second column does not have a name, just a number as this is what I call a derived column, its value is created within the SQL statement.

If I wanted the same information in a random order I would use:

01  SELECT PID, FIRST_NAME || ' ' || LAST_NAME
02    FROM PERSON 
03   ORDER BY RANDOM()

Line 3: I have added the ORDER BY to order by a generated random number, created by the RANDOM scalar function.

The first five results are:

PID    00002
----   ---------------
  38   JAMES BULLOCK
  37   JACK WILSON
  15   JOHNNY BERRY
  16   BRIAN BIRCH
  24   JOHN WALTON

RANDOM and RAND are synonyms, therefore, I could change the third line of the statement:

01  SELECT PID, FIRST_NAME || ' ' || LAST_NAME
02    FROM PERSON 
03   ORDER BY RAND()

The first five results are:

PID    00002
----   ---------------
  41   CHARLES RAMSDEN
  33   GEORGE LYDON
  14   JOHN ASTON JR
  29   JACK SILCOCK
  35   THOMAS PARKER

If no parameter is given to the scalar function a default seeding value is given by Db2 for i. Some sources I found claims that if I was to run scalar function with no parameter the returned results might be the same the first time it is used in every new job.

I decide to create my own seeding value, something I thought would be pretty unlikely to return the same value more than once. I decided to use the microseconds returned as part of a timestamp. For that I would use the MICROSECOND scalar function with the current timestamp:

  MICROSECOND(CURRENT_TIMESTAMP)

I would use that in my statement thus:

01  SELECT PID, FIRST_NAME || ' ' || LAST_NAME
02    FROM PERSON 
03   ORDER BY RAND(MICROSECOND(CURRENT_TIMESTAMP))

Line 3: The RAND's parameter is MICROSECOND from the current timestamp.

Top five results are:

PID    00002
----   ---------------
  20   JOHN DOWNIE
   2   JACK CROMPTON
  22   STAN PEARSON
  31   WILLIAM DALE
  19   FRANK CLEMPSON

If I was using this in a production environment I would "clean up" the statement I have been using to give the two columns in the results meaningful column labels. I use the LIMIT to limit the number to a certain number of results. This saves Db2 having to sort the entire table before returning the results, not a big deal with my small Person file but with a large file this would save time.

My new statement would be:

01  SELECT PID AS "Id",
02         FIRST_NAME || ' ' || LAST_NAME AS "Name"
03    FROM PERSON 
04   ORDER BY RAND(MICROSECOND(CURRENT_TIMESTAMP))
05   LIMIT 5

Lines 1 and 2: I have given both of the columns meaningful headings.

Line 5: The LIMIT restricts the number of returned results to the first five.

This time I can show all of the results:

Id     Name
----   ---------------
  39   STANLEY GALLIMORE
  32   LAL HILDITCH
   7   JACKIE BLANCHFLOWER
  16   BRIAN BIRCH
  27   THOMAS JONES

Most of the time I am asked by auditors to gather a random of sample of results for some information the sample size is usually ten to twenty results. This is a great way to give them the information quickly, and with a method they should be able to understand.

 

This article was written for IBM i 7.6 TR1 and 7.5 TR7.

No comments:

Post a Comment

To prevent "comment spam" all comments are moderated.
Learn about this website's comments policy here.

Some people have reported that they cannot post a comment using certain computers and browsers. If this is you feel free to use the Contact Form to send me the comment and I will post it for you, please include the title of the post so I know which one to post the comment to.