I'm using SQL Server 2017. I have a table like the following:
+------+----------+-----+
| DATE | CATEGORY | SUM |
+------+----------+-----+
| 1/20 | A | 4 |
| 1/20 | B | 7 |
| 1/20 | C | 3 |
| 1/21 | A | 6 |
| 1/21 | B | 2 |
| 1/21 | C | 9 |
+------+----------+-----+
I want to transform it into this, with a column for each category and single row for each date:
+------+----------+----------+----------+
| DATE | Sum of A | Sum of B | Sum of C |
+------+----------+----------+----------+
| 1/20 | 4 | 7 | 3 |
| 1/21 | 6 | 2 | 9 |
+------+----------+----------+----------+
The catch is the number of columns will vary between runs of the query. Is there a way to write SQL to do generate a column for each category without having to define each category individually in the select statement? The number of categories will vary between 30-50, I'd like to be able to automatically return only columns corresponding to a category. Is this possible? I'm not sure where to start. Thanks so much!