Graphing with a multi-value query variable

I have some time series telematics data from several vehicles in one table.

I would like to be able to select which vehicle the data is from and have the option of plotting data from more than one vehicle at once.

I created a variable called “Vehicles” that is a list of the unique vehicles. I use it in a a query like this:

SELECT "Session Start Time", "Power Discharged (Max) (kW)", "Power Charged (Max) (kW)", "Idle Power (Avg) (kW)" FROM sessions where "Vehicle Name" = '$Vehicles'

This works fine for when I select a single value, but when I select multi-value option for the variable, the query doesn’t work.

I get an error like this:

db query error: pq: trailing junk after numeric literal at or near "7g"

7g is the start of one on the vehicle names in the ‘Vehicles’ variable

click in query inspector, click refresh and it should provide you with a hint with thr query it generates

Thanks for the tip @yosiasz . I did what you suggest and got the following, but not sure how to interpret it:

{
“request”: {
“url”: “api/ds/query?ds_type=grafana-postgresql-datasource&requestId=Q574”,
“method”: “POST”,
“data”: {
“queries”: [
{
“refId”: “B”,
“datasource”: {
“type”: “grafana-postgresql-datasource”,
“uid”: “ddu1ndh1e97gge”
},
“rawSql”: "SELECT "Session Start Time", "Economy (while Driving) (kWh/km)" FROM sessions where "Vehicle Name" = ‘‘7gen2’,‘7gen3’,‘7gen4’,‘pacepulse1’’ ",
“format”: “table”,
“datasourceId”: 1,
“intervalMs”: 43200000,
“maxDataPoints”: 738
}
],
“from”: “1691181000500”,
“to”: “1732127398500”
},
“hideFromInspector”: false
},
“response”: {
“results”: {
“B”: {
“error”: “db query error: pq: trailing junk after numeric literal at or near "7g"”,
“errorSource”: “”,
“status”: 500,
“frames”: [
{
“schema”: {
“refId”: “B”,
“meta”: {
“typeVersion”: [
0,
0
],
“executedQueryString”: "SELECT "Session Start Time", "Economy (while Driving) (kWh/km)" FROM sessions where "Vehicle Name" = ‘‘7gen2’,‘7gen3’,‘7gen4’,‘pacepulse1’’ "
},
“fields”:
},
“data”: {
“values”:
}
}
],
“refId”: “B”
}
}
}
}

@yosiasz I was able to get it to work by changing the where clause to

FROM sessions where "Vehicle Name" in ($Vehicles)

Thanks for the tip!

1 Like