In SQL Server does anyone know of a nice way to merge/flatten multiple rows of data into the fewest possible rows containing only the distinct non null values.
I.e.
A dataset like this:
As this:
If it helps, the before dataset is a pivoted line listing but without the aggregate. I can't aggregate it during the pivot as I want to keep each of the distinct values and not take the MAX or MIN.
The only way I can think of doing it involves splitting the data up and joining it all back together which wont be very efficient.
Best Answer
Your data appears to lack any relationship between the various column values. If you can define this relationship, you can
PIVOT
the data appropriately.For example, if you simply want to align the data based on the order of the value (based on your default collation), you could use: