[英]Get result from a table with a condition and then use the result of it to check another condition in the same table and get the output
我有一个由多列组成的表。
表由如下数据组成
组号 | 最大时间 | 更新时间 |
---|---|---|
A22 | 20221502 | 20221702 |
A22 | 20212502 | 20221702 |
A22 | 20212502 | 20221702 |
我用类似的条件查询该表
Select Group no from cnt where maxtime<=updatedtime
output 来A22
现在我想使用这个 output 再次查询同一个表,并在 where 子句中使用表的其他列的条件获得 A22 的计数为 3。
就像是
Select count(group no)
From cnt
Where (effdate<candate)
Effdate 和candate 是同一个表的列。
使用GROUP BY
获取每个组的计数。 并将这两个条件与AND
结合起来。
SELECT group_no, COUNT(*)
FROM cnt
WHERE maxtime <= updatedtime
AND effdate < candate
GROUP BY group_no
我想你想要这个:为每个 group_no 计算 effdate < candate 的行,其中存在 maxtime <= updatedtime 的行。
Select group_no, count(*)
from cnt
where effdate < candate
and group_no in (select group_no from cnt where maxtime <= updatedtime)
group by group_no
order by group_no;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.