繁体   English   中英

组合单行记录数的查询结果

[英]Combining query results for number of records in a single row

我是 SQL 新手。 我有一个表,它有十亿条记录,有多个列,比如说 a、b、c、d、e、f。

我想为具有特定条件的每列的记录数创建一个查询,下一列是该结果占记录总数的百分比,然后是具有相同条件的 b、c、d 和 e 列。 我想在一行中获取查询的输出。

所以,对于 a 列,我想要的查询是这样的:

select count(a) from table 1
where a >0 and date > '2020-01-01'

下一列将是上述结果相对于使用这样的查询的记录总数的百分比

select count(*) from table 1
where date >'2020-01-01'

第 3 列将是此查询的结果

select count(b) from table 1
where a >0 and date > '2020-01-01'

第 4 列将是 b 列具有 avove 条件的记录数相对于总记录数的百分比

c、d 和 e 列也是如此。

预期的结果是这样的

a 列有条件的记录数,a 有条件相对于总记录的百分比,b 列有条件的记录数,b 有条件相对于总记录的百分比,...... …………

应该如何正确编写查询?

使用条件聚合:

select
    avg(case 
        when a > 0 and date > '2020-01-01' then 1.0
        when date > '2020-01-01' then 0
    end) avg_a,
    avg(case 
        when b > 0 and date > '2020-01-01' then 1.0
        when date > '2020-01-01' then 0
    end) avg_b
...
from mytable

这使您能够根据case表达式中包含的各种条件计算指标。 以上计算列a , b , ... 大于 0 且日期为 2020 年或以上的记录与仅满足日期条件的记录数的比率。 其他记录被忽略。

请注意,如果所有case的日期条件都相同,您可以将其移动到where子句中,这简化了逻辑:

select
    avg(case when a > 0 then 1.0 else 0 end) avg_a,
    avg(case when b > 0 then 1.0 else 0 end) avg_b
...
from mytable
where '2020-01-01'

只需使用条件聚合。 这是一种方法:

select avg(case when a > 0 then 1.0 else 0 end)
from table 
where date >'2020-01-01'

或者,BigQuery 中的简短内容是什么:

select countif(a > 0) / count(*)
from table
where date >'2020-01-01' ;

您可以对其余的列重复其中任何一个。

暂无
暂无

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

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