I am sure there must be a relatively straightforward way to do this, but it is escaping me at the moment. Suppose I have a SQL table like this:
+-----+-----+-----+-----+
| A | B | C | D |
+=====+=====+=====+=====+
| a | b | 3 | 100 | << a,b
+-----+-----+-----+-----+
| a | c | 3 | 60 | << a,c
+-----+-----+-----+-----+
| a | b | 4 | 50 | << a,b
+-----+-----+-----+-----+
| a | b | 5 | 30 | << a,b
+-----+-----+-----+-----+
| d | b | 3 | 35 | << d,b
+-----+-----+-----+-----+
| a | c | 2 | 40 | << a,c
+-----+-----+-----+-----+
Now, I want to know how many times each combination of values for columns A and B appear, then it can hold data column D based on grouping by column C in single row. So, in this example, I want an output something like this:
+-----+-----+-----+-----+-----+
| A | B | C3 | C4 | C5 |
+=====+=====+=====+=====+=====+
| a | b | 100 | 50 | 30 | << a,b
+-----+-----+-----+-----+-----+
| a | c | 60 | 0 | 0 | << a,c
+-----+-----+-----+-----+-----+
| d | b | 35 | 0 | 0 | << d,b
+-----+-----+-----+-----+-----+
What would be the SQL to determine that? I feel like this must not be a very uncommon thing to want to do.
Thanks!