仅在满足另一列条件时计数

时间:2019-07-02 09:57:22

标签: sql oracle

只有在满足其他列条件的情况下,我才必须计算ID。 ID不是唯一的,可能包括几个步骤。

表格如下:

rownum | ID | key   | result

1      |100 | step1 | accepted
2      |100 | step2 | accepted
3      |100 | step3 | transfer
4      |101 | step0 | accepted
5      |101 | step1 | accepted
6      |101 | step2 | rejected
7      |102 | step0 | accepted
8      |102 | step1 | accepted
9      |103 | step1 | rejected
10     |104 | step1 | rejected
11     |104 | step1 | rejected
12     |104 | step1 | rejected

在该示例中,我有5个ID(但在实际表中有数千个ID),并且我必须在满足条件的情况下仅计数ID。条件非常简单:键<>'step0',因此我的COUNT脚本应返回值3。

如果我尝试

COUNT ID
FROM myTable
WHERE key <> 'step0'

它返回错误的值,因为WHERE子句应用了先前的COUNT

任何想法表示赞赏。

5 个答案:

答案 0 :(得分:2)

这是一种不需要嵌套聚合函数并且不需要子查询的方法:

select (count(distinct id) -
        count(distinct case when key = 'step0' then id end)
       )
from mytable;

答案 1 :(得分:1)

尝试使用不存在的相关子查询

select count(distinct ID)
from tablename a 
   where not exists (select 1 from tablename b where a.id=b.id and key = 'step0')

答案 2 :(得分:0)

使用不同的

select COUNT (distinct ID)
FROM myTable
WHERE ID not in ( select id from myTable where key = 'step0' and id is not null)

答案 3 :(得分:0)

与具有having子句的分组一起使用

select sum(count(distinct ID)) as "Count"
  from myTable
 group by ID
 having sum(case when key = 'step0' then 1 else 0 end)=0;
-- or "having sum( decode(key,'step0',1,0) ) = 0" is also possible specific to Oracle

Count
-----
  3

例如使用反向逻辑,来自key = 'step0'

Demo

答案 4 :(得分:0)

这应该有效:

> df_updated
              S1  S2  S3  S4  S5  S6
Gene1         1   1   1   1   2   1
Gene1         1   1   1   1   1   1
Gene1         1   1   1   1   1   1
Gene1         2   1   1   1   1   1
Gene1         1   1   1   1   1   1
Gene1         1   1   1   1   1   1
Gene1        NA  NA   1   1   1   1
Gene1         1   2   1   1   1   1
Gene1         1   1   1   1   1   1
Gene1         2   2   1   1   1   1
Gene1         1   1   1   1   1   1

外部df_updated <- as.data.frame(lapply(df, function(x) if (x>=df$`2.5%` & x<=df$`97.5%`) {x==1} else {x==2})) Error in if (x >= df$`2.5%` & x <= df$`97.5%`) { : missing value where TRUE/FALSE needed In addition: There were 50 or more warnings (use warnings() to see the first 50) 适用于整个查询,包括SELECT COUNT(COUNT(DISTINCT id)) num_ids FROM your_table GROUP BY id HAVING MIN(CASE WHEN key = 'step0' THEN key END) IS NULL; 的聚合-将其删除,您会看到三行的值为1。