This question is similar to what I need to do, but the voted answer only partially solves the problem. Consider this table:
ID | TEST | RESULT
001 | AAA | +
001 | BBB | +
002 | AAA | +
003 | BBB | +
I would like output like this:
ID | AAA | BBB
001 | + | +
002 | + |
003 | | +
The solution I mentioned above provides the same data as its output, but does not “skip over” the empty columns, as for 003. Therefore, it is not possible to determine which test a + result refers to. I’m using PostgreSQL 8.4.9.
PS: Is a solution in pure SQL possible? Would this need to be coded in PL/pgSQL instead?
For a quick and dirty solution you can use this:
If you need that more dynamic consider using
crosstabfunction in theSELECT.