表名: Account
列:
ParentID RecordType
ABC100 X
ABC100 Y
EFG100 x
HIJ200 x
JKL200 Y
UVW100 Y
UVW100 X如何拉取ParentID ABC100和UVW100?我只想拉出那些记录类型既有X又有Y的父代?
发布于 2020-03-04 05:22:54
您可以使用聚合。如果您没有副本:
select parentid
from t
where recordtype in ('X', 'Y')
group by parentid
having count(*) = 2;或者,如果表中有重复项:
having min(recordtype) <> max(recordtype)发布于 2020-03-04 05:25:05
如果表的大小不是很大,并且内部查询的大小不超过2100,则可以使用内部查询。
SELECT parentId
FROM Account
WHERE recordType = 'X'
AND parentId IN
(SELECT parentId FROM Account WHERE recordType = 'Y')发布于 2020-03-04 05:31:43
为了使您的选择更加完善,您还可以组合使用WHERE和WHERE EXISTS。
select
ParentID
from
account as o
where
RecordType = 'X'
and
exists (select 1
from account as i
where
i.ParentID = o.ParentID
and
i.RecordType = 'Y'
);https://stackoverflow.com/questions/60515705
复制相似问题