平均每小时平均转置SQLite行和列 [英] Transposing SQLite rows and columns with average per hour

查看:80
本文介绍了平均每小时平均转置SQLite行和列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在SQLite中有一个名为param_vals_breaches的表,如下所示:

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

id    param       queue       date_time              param_val    breach_count
1     c           a           2013-01-01 00:00:00    188          7
2     c           b           2013-01-01 00:00:00    156          8
3     c           c           2013-01-01 00:00:00    100          2
4     d           a           2013-01-01 00:00:00    657          0
5     d           b           2013-01-01 00:00:00    23           6
6     d           c           2013-01-01 00:00:00    230          12
7     c           a           2013-01-01 01:00:00    100          0
8     c           b           2013-01-01 01:00:00    143          9
9     c           c           2013-01-01 01:00:00    12           2
10    d           a           2013-01-01 01:00:00    0            1
11    d           b           2013-01-01 01:00:00    29           5
12    d           c           2013-01-01 01:00:00    22           14
13    c           a           2013-01-01 02:00:00    188          7
14    c           b           2013-01-01 02:00:00    156          8
15    c           c           2013-01-01 02:00:00    100          2
16    d           a           2013-01-01 02:00:00    657          0
17    d           b           2013-01-01 02:00:00    23           6
18    d           c           2013-01-01 02:00:00    230          12

我想编写一个查询,该查询将向我显示一个特定队列(例如" a "),该队列的平均 param_val breach_count 每个参数按小时计算.因此,转置数据以获得如下所示的内容:

I want to write a query that will show me a particular queue (e.g. "a") with the average param_val and breach_count for each param on an hour by hour basis. So transposing the data to get something that looks like this:

Results for Queue A

         Hour 0         Hour 0              Hour 1         Hour 1              Hour 2         Hour 2
param    avg_param_val  avg_breach_count    avg_param_val  avg_breach_count    avg_param_val  avg_breach_count
c        xxx            xxx                 xxx            xxx                 xxx            xxx
d        xxx            xxx                 xxx            xxx                 xxx            xxx  

这可能吗?我不确定该怎么做.谢谢!

is this possible? I'm not sure how to go about it. Thanks!

推荐答案

SQLite没有PIVOT函数,但是您可以使用带有CASE表达式的聚合函数将行变成列:

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;

请参见带有演示的SQL小提琴

这篇关于平均每小时平均转置SQLite行和列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆