I’m looking for a query that allows me to “flatten” rows with the same ID:
- Values from the second column are used as the new tables column names
- Values from the third column determine the values in the new table.
As an example:
A table looks like this.
ID year event
------------------------
Guid1 2005 A
Guid1 2010 B
Guid2 2020 C
I want it to look like..
ID 2005 2010 2020
Guid1 A B
Guid2 C
Any ideas on how to do this?
I assume (id, year) combination is unique? Otherwise not sure what output you expect. If unique, it looks like this:
The above means you’ll have to repeat the
MIN(IF(...))statement once for each desired year. There is no way in SQL to do this automatically: SQL does not mix data and metadata. You cannot dynamically specify columns in a query. Either you use “*” or you specify the exact columns.