I have postgres table with three variables: datetime, robot_id and energy_percent. Now at chart I have energy_percent
and robot_id
. How to group records from this table by robot_id
? Currently at this table I have records with 3 different robot_id
, so I want see 3 lines on this chart.
Edit:
I have one table with a lot records, at column robot_id
I have ids. I need one line chart per one unique robot_id
. If table contains x
uniquerobot_id
I expect x
lines on chart. Here is sample at excel: on left is wrong chart, at right is correct chart:
And moreover one thing: on excel where is no data I have 0, but I need no point.
Edit2: I can do it with a lot of series as below, but it should be universal (idk how many unique robot_id
will be) and done with one Query (A) not a lot of queries (A,B,C, etc.)
Edit 3: When I apply code:
SELECT
$__timeGroupAlias(time_created, $__interval),
robot_id AS "metric",
AVG(energy_percent) AS "value"
FROM api_robotlog
WHERE
$__timeFilter(time_created)
GROUP BY 1,2
ORDER BY 1
I have 2 plots: energy_percent(time)
and robot_id(time)
. There is no a lot of plots energy_percent(time, robot_id)
.
Edit 5: Using @Jan Garaj solution it working. Two charts, with two similar queries: TOP chart query:
SELECT
$__timeGroupAlias(time_created, $__interval),
robot_id AS "metric",
AVG(energy_percent) AS "value"
FROM api_robotlog
WHERE
$__timeFilter(time_created)
GROUP BY 1,2
ORDER BY 1
BOTTOM chart query:
SELECT
$__timeGroupAlias(time_created, $__interval),
robot_id::varchar AS "metric",
AVG(energy_percent) AS "value"
FROM api_robotlog
WHERE
$__timeFilter(time_created)
GROUP BY 1,2
ORDER BY 1
CodePudding user response:
Just group it properly. I would switch to text edit more and write SQL manually. For example:
SELECT
$__timeGroupAlias(time_created, $__interval),
robot_id AS "metric",
AVG(energy_percent) AS "value"
FROM api_robotlog
WHERE
$__timeFilter(time_created)
GROUP BY 1,2
ORDER BY 1