简体   繁体   English

来自单个表的sqlite3连接查询

[英]sqlite3 join query from single table

A sqlite3 db table contains device perf data with two columns.. device and value. sqlite3 db表包含带有两列的设备性能数据。设备和值。

Content is something like this 内容是这样的

deviceA|50
deviceB|75
deviceA|125
deviceB|25
deviceA|99
deviceB|10
deviceA|101
and on and on

For each device 对于每个设备

  1. I want to know how many entries are in the table (total) 我想知道表中有多少个条目(总计)
  2. I want to know how mnay entries are over threshold of 100 (overthreshold) 我想知道mnay条目的阈值如何超过100(阈值)
  3. I want to know how many entries are under threshold of 100 (underthreshold) 我想知道有多少个条目的阈值低于100(阈值下限)
  4. I want to know the percent of total entries under threshold (percent) 我想知道低于阈值的总条目的百分比(百分比)

This is my query so far 到目前为止,这是我的查询

    select distinct(total.device),
       total.count,
       overthreshold.count,
       round(((total.count*1.0 - overthreshold.count)/total.count),4)*100
    from
    (select device,count(*) as count from perfdata group by device) as total
      inner join (
        select device,count(*) as count from perfdata where value>100 group by device
      ) as overthreshold
    group by overthreshold.device;

deviceA only results included here deviceA仅在此处包含结果

deviceA|2017|16|99.21

deviceA had 2017 entries in the table, 16 of which are > 100; deviceA在表中有2017个条目,其中16个> 100; 99.21% under threshold. 低于门槛的99.21%。

for all device/value combinations, output currently only shows those overthreshold as my query tells it to. 对于所有设备/值组合,当前输出仅显示我的查询所指示的那些阈值。

deviceB is never overthreshold and isn't in query output (100% under threshold). deviceB永远不会超过阈值,也不会出现在查询输出中(低于阈值100%)。

Any advice on where/how would I add in the 关于在哪里/如何添加的任何建议

    select device,count(*) as count from perfdata where value<100 group by device

statement to get underthreshold returned back for inclusion in my calculation? 返回阈值的语句返回以包含在我的计算中?

Thanks for any help. 谢谢你的帮助。

You want to use conditional aggregation. 您要使用条件聚合。 This is where you use the case statement along with the aggregation functions: 在这里,您可以使用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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM