Skip to content

Instantly share code, notes, and snippets.

@adlerweb
Last active December 30, 2023 17:25
Show Gist options
  • Save adlerweb/1f2bec2530870d2c08dcf57794db452b to your computer and use it in GitHub Desktop.
Save adlerweb/1f2bec2530870d2c08dcf57794db452b to your computer and use it in GitHub Desktop.
Grafana MySQL query to visualize Volkszähler-Data

Using Channel-IDs (that's not UUID)

SELECT
  timestamp/1000 as time_sec, 
  data.value as value, 
  properties.value as metric
FROM data
  LEFT JOIN properties ON (properties.entity_id = data.channel_id)
  LEFT JOIN entities ON (entities.id = data.channel_id)
WHERE 
  (channel_id = 1 || channel_id = 2 || channel_id = 3)  AND 
  timestamp >= $__unixEpochFrom()*1000 AND 
  timestamp <= $__unixEpochTo()*1000 AND
  properties.pkey = 'title'
ORDER BY timestamp ASC

Using channel names

SELECT
  timestamp/1000 as time_sec, 
  data.value as value, 
  properties.value as metric
FROM data
  LEFT JOIN properties ON (properties.entity_id = data.channel_id)
  LEFT JOIN entities ON (entities.id = data.channel_id)
WHERE 
  (properties.value = "Flur" || properties.value = "Werkstatt" ) AND
  timestamp >= $__unixEpochFrom()*1000 AND 
  timestamp <= $__unixEpochTo()*1000 AND
  properties.pkey = 'title'
ORDER BY timestamp ASC

Using UUID

SELECT
  timestamp/1000 as time_sec, 
  data.value as value, 
  properties.value as metric
FROM data
  LEFT JOIN properties ON (properties.entity_id = data.channel_id)
  LEFT JOIN entities ON (entities.id = data.channel_id)
WHERE 
  (uuid = "12345678-1234-1234-1234-1234567890ab" || uuid = "12345678-1234-1234-1234-1234567890ac" ) AND
  timestamp >= $__unixEpochFrom()*1000 AND 
  timestamp <= $__unixEpochTo()*1000 AND
  properties.pkey = 'title'
ORDER BY timestamp ASC
@oriziena
Copy link

just in case somebody finds that like me...

you can significant improve the access by changing the WHERE clause:

SELECT
  timestamp/1000 as time_sec, 
  data.value as StundenProTag, 
  properties.value as metric
FROM data
  LEFT JOIN properties ON (properties.entity_id = data.channel_id)
  LEFT JOIN entities ON (entities.id = data.channel_id)
WHERE 
  (uuid = "02363c80-4789-11ea-845f-7d8b8b898c67") AND
  timestamp >= 1000 * $__unixEpochFrom() AND 
  timestamp <= 1000 * $__unixEpochTo() AND
  properties.pkey = 'title'
ORDER BY timestamp ASC

@adlerweb
Copy link
Author

Good catch, I updated the gist according to your suggestion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment