I'm building a dashboard and I need to return certain columns from a table depending on a sensor's choice. The user selects a sensor and the database returns some columns depending on which sensor the user chose.
The approach I had in mind was something simple like this:
if ${sensor} = '1' then
select time, temperature from tbl;
elsif ${sensor} = '2' then
select time, humidity from tbl;
else
--return nothing
end if;
My table is made up of a timestamp column and several other float columns, each of them containing the value of a physical variable at that certain time (temperature, pressure, humidity, height and weight). So if the user selects for example Sensor 1, a thermometer, my query should return the time and temperature columns. Some of the sensors could also return more than one physical variable, so I would need to return a table with three columns, time, height and weight.
I'm using Grafana and I'm able to read the chosen variable without any problem, but I am unable to build a table with the columns of my choice depending on this value.
I thought about writing a function that returned a table, but this is way too far from my SQL knowledge and I was pretty sure this could be done with plain SQL.
Any ideas?