我正在寻找一种易于阅读的智能SQL查询(SQLite引擎)来聚合列中的数据。通过一个例子更容易解释:
数据表:
*** Settings ***
Library Collections
*** Test Cases ***
Test AAA
${aaaaa} My Keyword A=aaa B=bbb C=ccc
${bbbbb} My Keyword A=aaa B=bbb C=ccc D=ddd
*** Keywords ***
My Keyword
[Arguments] @{args}
${resp} Create Dictionary ${args}
[Return] ${resp}
预期结果集:httpcode列,按时间编码。在这个例子中,时间聚集是0.2秒(但它可以在一秒钟或10秒内聚集)。我只对某些预期的http_code感兴趣:
id elapsedtime httpcode
1 0.0 200
2 0.1 200
3 0.3 301
4 0.6 404
5 1.0 200
6 1.1 404
7 1.2 500
" time"并非强制性的。要连续的。在前面的示例中,可以删除时间为0.6和0.6的行。
目前,我可以通过执行4个不同的请求(一个通过http代码)来完成此操作,并在我的开发应用程序中重新聚合结果:
time code_200 code_404 code_500 code_other
0.0 2 0 0 0
0.2 0 0 0 1
0.4 0 1 0 0
0.6 0 0 0 0
0.8 0 0 0 0
1.0 1 1 1 0
但我非常确定我可以用一个查询来实现这一目标。不幸的是,我没有掌握UNION关键字......
有没有办法在单个SELECT中获取这些数据?
参见SQLFiddle:http://sqlfiddle.com/#!5/2081f/3/1
答案 0 :(得分:2)
找到一个比我原来的帖子更好的解决方案,我会留下来,以防你好奇。这是更好的解决方案:
with t1 as (
select
0.2 * cast (elapsedtime/ 0.2 as int) as time,
case httpcode when 200 then 1 else 0 end code_200,
case httpcode when 404 then 1 else 0 end code_404,
case httpcode when 500 then 1 else 0 end code_500,
case when httpcode not in (200, 404, 500) then 1 else 0 end code_other
from test
)
select time,
sum(code_200) as count_200,
sum(code_404) as count_404,
sum(code_500) as count_500,
sum(code_other) as count_other
from t1
group by time;
旧解决方案:
这在眼睛上可能不太容易,但它或多或少都有效(只有你想要的输出和我得到的结果之间的差异是没有值的时间分组(在你的例子中是0.6和0.8)被省略:
with
t_all as (select
0.2 * cast (elapsedtime/ 0.2 as int) as time, count(id) as total
from test
group by time
),
t_200 as (select
0.2 * cast (elapsedtime/ 0.2 as int) as time, count(id) as code_200
from test
where (httpcode=200)
group by time),
t_404 as (select
0.2 * cast (elapsedtime/ 0.2 as int) as time, count(id) as code_404
from test
where (httpcode=404)
group by time),
t_500 as (select
0.2 * cast (elapsedtime/ 0.2 as int) as time, count(id) as code_500
from test
where (httpcode=500)
group by time),
t_other as (select
0.2 * cast (elapsedtime/ 0.2 as int) as time, count(id) as code_other
from test
where (httpcode not in (200, 404, 500))
group by time)
select
t_all.time,
total,
ifnull(code_200,0) as count_200,
ifnull(code_404,0) as count_404,
ifnull(code_500,0) as count_500,
ifnull(code_other,0) as count_other
from t_all
left join t_200 on t_all.time = t_200.time
left join t_404 on t_all.time = t_404.time
left join t_500 on t_all.time = t_500.time
left join t_other on t_all.time = t_other.time;
答案 1 :(得分:0)
它可能对你有帮助
select
0.2 * cast (elapsedtime/ 0.2 as int) as time, count(id) as code_200,
0 as code_404,
0 as code_500,
0 as code_other
from
test
where (httpcode=200)
group by time
union
select
0.2 * cast (elapsedtime/ 0.2 as int) as time,0 as code_200,
count(id) as code_404,
0 as code_500,
0 as code_other
from
test
where (httpcode=404)
group by time
union
select
0.2 * cast (elapsedtime/ 0.2 as int) as time,0 as code_200,
0 as code_400,
count(id) as code_500,
0 as code_other
from
test
where (httpcode=500)
group by time
union
select
0.2 * cast (elapsedtime/ 0.2 as int) as time,0 as code_200,
0 as code_400,
0 as code_500,
count(id) as code_other
from
test
where (httpcode<>200 and httpcode <> 404 and httpcode <> 500)
group by time