Skip to content
Advertisement

Select equal number of random rows with respect to a column

Consider I have a table like this

How can I select rows from it so that I have equal number of 1s and 0s, like below can be a result

The rows removed/left out are at random and deleting from an existing table would work as well.

Advertisement

Answer

For each col2 partition, you can give each row a row number and then find those rows where there is only one instance of the row number and delete them:


If you just want to SELECT the rows then you can use a similar technique:

db<>fiddle here

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