Suppose I have a table in Postgres called listings that looks like this:
| id | neighborhood | bedrooms | price |
|---|---|---|---|
| 1 | downtown | 0 | 189000 |
| 2 | downtown | 3 | 450000 |
| 3 | riverview | 1 | 300000 |
| 4 | riverview | 0 | 250000 |
| 5 | downtown | 1 | 325000 |
| 6 | riverview | 2 | 350000 |
etc.
How do I write a crosstab query that shows the average price per bedrooms as the columns and neighborhoods as the rows?
The output of the query should have the following format:
| neighborhood | 0 | 1 | 2 | 3 |
|---|---|---|---|---|
| downtown | 189000 | 325000 | - | 450000 |
| riverview | 250000 | 300000 | 350000 | - |
etc.