I have the following result :
-------------------------
| dept | Active request |
-------------------------
| AFG | 3 |
| AGO | 4 |
| KMN | 1 |
| MOL | 1 |
| POD | 2 |
| SUD | 2 |
-------------------------
How can I tranform it to have something like
--------------------------------------------------------------
| Title | AFG | AGO | KMN | MOL | POD | SUD | TOTAL |
--------------------------------------------------------------
| Active Request | 3 | 4 | 1 | 1 | 2 | 2 | 13 |
--------------------------------------------------------------
Here is my fiddle http://sqlfiddle.com/#!9/b51a03/3
2
Answers
You could use a single pivot query:
This can be done with the postgresql crosstab tablefunc.
You can find an detailed explanation at: Pivot Tables in PostgreSQL Using the Crosstab Function