I have this table:
+------+----+-------+
| Time | Id | Value |
+------+----+-------+
| 1 | 1 | 0 |
+------+----+-------+
| 2 | 1 | 10 |
+------+----+-------+
| 3 | 1 | 20 |
+------+----+-------+
| 1 | 2 | 0 |
+------+----+-------+
| 2 | 2 | -10 |
+------+----+-------+
| 3 | 2 | -20 |
+------+----+-------+
| 1 | 3 | 0 |
+------+----+-------+
| 2 | 3 | 0 |
+------+----+-------+
| 3 | 3 | 0 |
+------+----+-------+
I want to create an SQL select statement that would return me this table:
+------+--------+--------+--------+
| Time | Value1 | Value2 | Value3 |
+------+--------+--------+--------+
| 1 | 0 | 0 | 0 |
+------+--------+--------+--------+
| 2 | 10 | -10 | 0 |
+------+--------+--------+--------+
| 3 | 20 | -20 | 0 |
+------+--------+--------+--------+
- Q: Why I would not change my structure in the first place?
- A: The ID's are linked to IoT device details and they are added dynamically (meaning that customer will one day have 40 of devices working and other 70).
What I want to achieve? The SQL statement should be working with the ID's dynamically (meaning that the number of returned columns will change based on how many different ID's there are).
What I have tried? I have tried various SQL statements but my knowledge is fairly limited and I can't do it on my own ... Group by time gives me ID and Value column in only aggregate form.
Other info:
- PostgresSQL is running on Supabase platform
- PostgresSQL version is 14
- My front end is Grafana Cloud. I am trying to get the result table into chart. And I can get the data there only through SQL API calls.