Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
551 views
in Technique[技术] by (71.8m points)

sql - Transposing SQLite rows and columns with average per hour

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

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...