I have a medium sized database housed in a star-schema with four outer branches of the star. For one particular piece of visualization software I'm evaluating, I need to query this database into an SPSS file with one of the outer branches pivoted into columns. I'm new to PostgreSQL, so maybe there's some simple way I'm missing.
Here's some example data (with only 2 outer branch tables - attitude & person):
mainTable
id | attitudeKey | response | personKey
1 | 4 | 3 | 2
2 | 1 | 2 | 2
3 | 4 | 1 | 1
4 | 1 | 4 | 1
5 | 5 | 3 | 1
attitudeTable
attitudeKey | attitudeName
4 | happy
1 | quiet
5 | grumpy
personTable
personKey | personName | personAge
1 | Bob | 35
2 | Amy | 30
And here is what the final result of the query needs to look like:
personKey | happy | quiet | grumpy | personName | personAge
1 | 1 | 4 | 3 | Bob | 35
2 | 3 | 2 | | Amy | 30
I'm sure there's a way to get this query from SQL, but I'm just not experienced enough to write the join and the pivot. Any help is greatly appreciated. Thanks.
I should mention that the ordering of the columns does NOT matter in the final result.
For now I'm just processing the data in R but this is VERY cumbersome and I feel like a simple SQL query with crosstabs would make my life so much easier.