Skip to content
Advertisement

SQL Select: Evenly Distribute Results Set

I have a table called TableA, with a Color column.

Nine rows total, if I select * from TableA order by Color, I get results set

how do I distribute the colors in a select statement, such that, the results set is

Note: This sample shows an equal number of each color. I’m hoping for a solution that could handle a non-equal number of each color.

Here’s another example:

possible results:

Advertisement

Answer

You can make use of a window function/subquery:

https://rextester.com/FIJDL68429

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