I'm using Postgresql and have this table:
| device_id | date | variable_name | pulses |
|---|---|---|---|
| 1 | 2021-03-29 | height | 10 |
| 1 | 2021-03-29 | speed | 20 |
| 1 | 2021-03-30 | height | 30 |
| 1 | 2021-03-30 | temperature | 40 |
| 2 | 2021-03-29 | height | 50 |
| 2 | 2021-03-29 | acceleration | 60 |
| 2 | 2021-03-29 | distance | 70 |
And want to query so I Group By device_id and date, and create columns by variable_name, so the table expected is:
| device_id | date | height | speed | temperature | acceleration | distance |
|---|---|---|---|---|---|---|
| 1 | 2021-03-29 | 10 | 20 | 0 | 0 | 0 |
| 1 | 2021-03-30 | 30 | 0 | 40 | 0 | 0 |
| 2 | 2021-03-29 | 50 | 0 | 0 | 60 | 70 |
Any idea of how to do this?
caseexpressions to do conditional aggregation.