When you have your dataset loaded, you can start constructing some queries to discover what your data tells you. This tutorial uses Timescale hyperfunctions to construct queries that are not possible in standard PostgreSQL.
In this section, you learn how to construct queries, to answer these questions:
When you have your database set up for energy consumption data, you can construct a query to find the median and the maximum consumption of energy on an hourly basis in a typical day.
Connect to the Timescale database that contains the energy consumption dataset.
At the psql prompt, use the Timescale Toolkit functionality to get calculate the fiftieth percentile or the median. Then calculate the maximum energy consumed using the standard PostgreSQL max function:
WITH per_hour AS (SELECTtime,valueFROM kwh_hour_by_hourWHERE "time" at time zone 'Europe/Berlin' > date_trunc('month', time) - interval '1 year'ORDER BY 1), hourly AS (SELECTextract(HOUR FROM time) * interval '1 hour' as hour,valueFROM per_hour)SELECThour,approx_percentile(0.50, percentile_agg(value)) as median,max(value) as maximumFROM hourlyGROUP BY 1ORDER BY 1;The data you get back looks a bit like this:
hour | median | maximum----------+--------------------+---------00:00:00 | 0.5998949812512439 | 0.601:00:00 | 0.5998949812512439 | 0.602:00:00 | 0.5998949812512439 | 0.603:00:00 | 1.6015944383271534 | 1.904:00:00 | 2.5986701108275327 | 2.705:00:00 | 1.4007385207185301 | 3.406:00:00 | 0.5998949812512439 | 2.707:00:00 | 0.6997720645753496 | 0.808:00:00 | 0.6997720645753496 | 0.809:00:00 | 0.6997720645753496 | 0.810:00:00 | 0.9003240409125329 | 1.111:00:00 | 0.8001143897618259 | 0.9
You can also check how energy consumption varies between weekends and weekdays.
Connect to the Timescale database that contains the energy consumption dataset.
At the psql prompt, use this query to find difference in consumption during the weekdays and the weekends:
WITH per_day AS (SELECTtime,valueFROM kwh_day_by_dayWHERE "time" at time zone 'Europe/Berlin' > date_trunc('month', time) - interval '1 year'ORDER BY 1), daily AS (SELECTto_char(time, 'Dy') as day,valueFROM per_day), percentile AS (SELECTday,approx_percentile(0.50, percentile_agg(value)) as valueFROM dailyGROUP BY 1ORDER BY 1)SELECTd.day,d.ordinal,pd.valueFROM unnest(array['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']) WITH ORDINALITY AS d(day, ordinal)LEFT JOIN percentile pd ON lower(pd.day) = lower(d.day);The data you get back looks a bit like this:
day | ordinal | value-----+---------+--------------------Mon | 2 | 23.08078714975423Sun | 1 | 19.511430831944395Tue | 3 | 25.003118897837307Wed | 4 | 8.09300571759772Sat | 7 |Fri | 6 |Thu | 5 |
You may also want to check the energy consumption that occurs on a monthly basis.
Connect to the Timescale database that contains the energy consumption dataset.
At the psql prompt, use this query to find consumption for each month of the year:
WITH per_day AS (SELECTtime,valueFROM kwh_day_by_dayWHERE "time" > now() - interval '1 year'ORDER BY 1), per_month AS (SELECTto_char(time, 'Mon') as month,sum(value) as valueFROM per_dayGROUP BY 1)SELECTm.month,m.ordinal,pd.valueFROM unnest(array['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']) WITH ORDINALITY AS m(month, ordinal)LEFT JOIN per_month pd ON lower(pd.month) = lower(m.month)ORDER BY ordinal;The data you get back looks a bit like this:
month | ordinal | value-------+---------+-------------------Jan | 1 |Feb | 2 |Mar | 3 |Apr | 4 |May | 5 | 75.69999999999999Jun | 6 |Jul | 7 |Aug | 8 |Sep | 9 |Oct | 10 |Nov | 11 |Dec | 12 |Optional To visualize this in Grafana, create a new panel, and select the
Bar Chart
visualization. Select the energy consumption dataset as your data source, and type the query from the previous step. In theFormat as
section, selectTable
.Optional Select a color scheme so that different consumptions are shown in different colors. In the options panel, under
Standard options
, change theColor scheme
to a usefulby value
range.
Keywords
Found an issue on this page?Report an issue or Edit this page in GitHub.