我有下表

时间:2016-10-20 10:47:27

标签: sql

我有一张表格如下

Parent ID   child   child status        
1   abc active
1   adf deleted
1   afc deleted

2   tyt deleted
2   rtf deleted

3   xyz deleted
3   dfr deleted
3   rty deleted
3   tyu deleted

4   poi active
4   plm deleted
4   piu deleted
4   pkg deleted

我需要返回所有子列都处于已删除状态的父级。

例如,在上表中我需要父2,3作为结果。

1 个答案:

答案 0 :(得分:2)

您可以使用聚合和having子句来执行此操作:

select parentid
from t
group by parentid
having min(childstatus) = max(childstatus) and
       min(childstatus) = 'deleted';