来自单个表的sqlite3联接查询
sqlite3 db表包含具有两列的设备性能数据。。设备和价值 内容是这样的 deviceA|50 deviceB|75 deviceA|125 deviceB|25 deviceA|99 deviceB|10 deviceA|101 and on and on 这里只包括设备测试结果 deviceA|2017|16|99.21 deviceA | 2017 | 16 | 99.21 deviceA在表中有2017个条目,其中16个大于100;99.21%低于阈值 对于所有设备/值组合,输出当前仅显示我的查询告诉它的那些被推翻的 deviceB永远不会被推翻,并且不在查询输出中(100%低于阈值) 有没有关于我应该在哪里/如何添加的建议来自单个表的sqlite3联接查询,sql,sqlite,Sql,Sqlite,sqlite3 db表包含具有两列的设备性能数据。。设备和价值 内容是这样的 deviceA|50 deviceB|75 deviceA|125 deviceB|25 deviceA|99 deviceB|10 deviceA|101 and on and on 这里只包括设备测试结果 deviceA|2017|16|99.21 deviceA | 2017 | 16 | 99.21 deviceA在表中有2017个条目,其中16个大于100;99.21%低于阈值 对于所有设备/值组合,输出当前
select device,count(*) as count from perfdata where value<100 group by device
选择设备,count(*)作为perfdata中的count,其中value要使用条件聚合。这是使用case
语句和聚合函数的地方:
select device, count(*) as TotalCount,
sum(case when value > 100 then 1 else 0 end) as OverThreshhold,
sum(case when value < 100 then 1 else 0 end) as UnderThreshhold,
100.0 * avg(case when value < 100 then 1.0 else 0.0 end) as PercentageUnder
from perfdata
group by device;
选择设备,计数(*)作为总计数,
求和(当值>100时,则为1,否则为0结束)作为翻转保持,
求和(当值小于100时,则为1,否则为0结束)作为欠阈值,
100.0*平均值(当值小于100时为1.0,否则为0.0结束)作为百分比
从perfdata
按设备分组;
工作正常。感谢Gordon。+1网站有些奇怪,我在发布我的回复之前没有看到你的回复,尽管你比我快了10分钟:)
select device, count(*) as TotalCount,
sum(case when value > 100 then 1 else 0 end) as OverThreshhold,
sum(case when value < 100 then 1 else 0 end) as UnderThreshhold,
100.0 * avg(case when value < 100 then 1.0 else 0.0 end) as PercentageUnder
from perfdata
group by device;