Skip to content
Advertisement

SQL query pivot, move values to top

I’ve created a PIVOT query and the results are fine, however. I would like to flatten the rows so to speak and move all values to the top of the list and NULLS to the bottom.

http://sqlfiddle.com/#!18/7d17d/6

Instead of this:

I would like to create something like this:

Advertisement

Answer

Change your ROW_NUMBER() to partition by the data source:

Your version is assigning a unique number to every row in the source data, which is why you get that number of rows back.

So the query is:

Here is the fiddle.

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