按时间计算和汇总数据
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了按时间计算和汇总数据相关的知识,希望对你有一定的参考价值。
我正在寻找一个美容易读的智能SQL查询(SQLite引擎)来集中列中的数据。用一个例子更容易解释:
数据表:
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
预期结果集:httpcode列,按时间编码。在这个例子中,时间聚集是0.2秒(但它可以在一秒钟或10秒内聚集)。我只对一些预期的http_code感兴趣:
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
“时间”不是强制性的。在前面的示例中,可以删除时间为0.6和0.6的行。
目前,我可以通过执行4个不同的请求(一个通过http代码)来完成此操作,并在我的开发应用程序中重新聚合结果:
select
0.2 * cast (elapsedtime/ 0.2 as int) as time, count(id) as code_200
from test
where (httpcode=200)
group by time
但我很确定我可以通过一个查询来实现这一目标。不幸的是,我没有掌握UNION关键字......
有没有办法在单个SELECT中获取这些数据?
请参阅SQLFiddle:http://sqlfiddle.com/#!5/2081f/3/1
答案
找到了比我原来的帖子更好的解决方案,我会留下来,以防你好奇。这是更好的解决方案:
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;
另一答案
它可能会帮助你
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
以上是关于按时间计算和汇总数据的主要内容,如果未能解决你的问题,请参考以下文章