Transposing SQLite rows and columns with average per hour

后端 未结 1 1937
广开言路
广开言路 2020-12-12 03:27

I have a table in SQLite called param_vals_breaches that looks like the following:

id    param       queue       date_time              param_val    breach_c         


        
相关标签:
1条回答
  • 2020-12-12 04:14

    SQLite does not have a PIVOT function but you can use an aggregate function with a CASE expression to turn the rows into columns:

    select param,
      avg(case when time = '00' then param_val end) AvgHour0Val,
      avg(case when time = '00' then breach_count end) AvgHour0Count,
      avg(case when time = '01' then param_val end) AvgHour1Val,
      avg(case when time = '01' then breach_count end) AvgHour1Count,
      avg(case when time = '02' then param_val end) AvgHour2Val,
      avg(case when time = '02' then breach_count end) AvgHour2Count
    from
    (
      select param,
        strftime('%H', date_time) time,
        param_val,
        breach_count
      from param_vals_breaches
      where queue = 'a'
    ) src
    group by param;
    

    See SQL Fiddle with Demo

    0 讨论(0)
提交回复
热议问题