Skip to content
Advertisement

Randomly flagging records in an Oracle Table

Given a table of IDs in an Oracle database, what is the best method to randomly flag (x) percent of them? In the example below, I am randomly flagging 20% of all records.

My current approach shown below works fine, but I am wondering if there is a more efficient way to do this?

Advertisement

Answer

You could use a ROW_NUMBER approach here:

Demo

Sample output for one run of the above query:

screen capture

Note that one of five records is flagged, which is 20%.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement