https://questdb.io logo
Title
h

Hexory

05/04/2023, 8:50 PM
Hi, I have a datapoint table created like this:
CREATE TABLE Datapoint (
  device_id LONG,
  sensor_id LONG,
  timestamp TIMESTAMP,
  value DOUBLE
) timestamp (timestamp) PARTITION BY YEAR;
I want to display this table in a Grafana panel. I want to have one series per sensor, but cant find the proper query syntax. Anyone can give me a hand? 🙂 I have this for now
SELECT
  timestamp as time,
  sensor_id,
  avg(value) as value
FROM Datapoint
WHERE $__timeFilter(timestamp) AND device_id=0
SAMPLE BY $__interval;
This give me a line for sensor_id and value. I want a line per sensor_id with their value. I tried with group and partition clause, but without sucess. Any idea? Thanks!
g

Gabriel Mocan

05/04/2023, 9:08 PM
Create a variable for
device_id
so you can query all devices in one query and plot them on chart.
SELECT DISTINCT device_id FROM Datapoint
Tick both “Multi-value” and “Include All option” then click on update; set the variable to All.
SELECT
  $__time(timestamp),
  sensor_id as metric,
  avg(value) as value
FROM Datapoint
WHERE $__timeFilter(timestamp) AND device_id in ($device_id)
SAMPLE BY $__interval;
h

Hexory

05/04/2023, 9:16 PM
thanks for the reply, trying that solution, i'm getting
pq: cannot compare TIMESTAMP with type CHAR
And i'm looking to have one series per sensor_id. and then you pick your device in the variable dropdown.
g

Gabriel Mocan

05/04/2023, 9:20 PM
This query should plot a different line for each sensor_id.
What version of Grafana you’re on?
h

Hexory

05/04/2023, 9:24 PM
v9.3.8
g

Gabriel Mocan

05/04/2023, 9:24 PM
Try to build the query in data explorer section, using the query builder
h

Hexory

05/04/2023, 9:25 PM
the query builder is just empty. No table found.
g

Gabriel Mocan

05/04/2023, 9:25 PM
You selected the proper datasource?
h

Hexory

05/04/2023, 9:25 PM
SELECT
  $__time(timestamp),
  sensor_id as metric,
  avg(value) as value
FROM Datapoint
WHERE $_timeFilter(timestamp) AND device_id = 0
SAMPLE BY $__interval;
even with this where device_id = 0, gives me nothing
g

Gabriel Mocan

05/04/2023, 9:26 PM
Well we should see something on data explorer. What do you see on QuestDB UI, can you show a print from
select * from Datapoint
?
h

Hexory

05/04/2023, 9:27 PM
image.png
image.png
g

Gabriel Mocan

05/04/2023, 9:29 PM
QuestDB runs postgres wire protocol on port 8812
Did you change that?
h

Hexory

05/04/2023, 9:30 PM
noo, well.
it runs inside my kubernetes cluster with a nodeport, but its connec ted to 8812
and I am getting data
g

Gabriel Mocan

05/04/2023, 9:31 PM
weird because your datasource config on grafana is pointing to localhost:5432
h

Hexory

05/04/2023, 9:31 PM
image.png
nah, ive just removed it to take the screenshot. for "security" that was the placeholder
g

Gabriel Mocan

05/04/2023, 9:31 PM
oh okay
did you try “sensor_id as metric” ?
In that same query you sent
h

Hexory

05/04/2023, 9:32 PM
yea, doesnt change anything except having a metric label
I tried group by, but without success
g

Gabriel Mocan

05/04/2023, 9:33 PM
image.png
Example from chart here
Can’t see too much difference
h

Hexory

05/04/2023, 9:36 PM
hummm, so metric is a special keyword for grouping? ill try googling it.
g

Gabriel Mocan

05/04/2023, 9:37 PM
Refer to Grafana’s official docs. https://grafana.com/docs/grafana/latest/datasources/postgres/
h

Hexory

05/04/2023, 9:57 PM
got some progress
image.png
soo combining my two columns was the trick
but got conversion issues for the In
voila, this works
SELECT
  $__time(timestamp),
  avg(value) AS value,
  device_id || sensor_id AS metric
FROM Datapoint
WHERE $__timeFilter(timestamp) AND cast(device_id as String) in ($device_id)
SAMPLE BY $__interval FILL(0)
g

Gabriel Mocan

05/04/2023, 10:02 PM
Nice