我认为这很容易,但出于某种原因,我无法绕过它。也许我累了。无论如何,我想返回一个状态代码为1和4的策略值。我有一个简单的查询:
select policiesid, eDeliveryStatusCd
from UserPolicyHistory
where PoliciesID is not null
and eDeliveryStatusCd in (1,4)
order by policiesid
请参阅10241的突出显示的policyid?我只想要那些返回,因为它有一个1和4的edeliverystatuscd。所以我想要一个状态代码为1和4的policyid只返回,忽略其余的。我认为应该很简单。我不知道为什么,但今天我无法弄清楚。任何帮助表示赞赏!
答案 0 :(得分:2)
只需添加group by
和having
:
select policiesid
from UserPolicyHistory
where PoliciesID is not null and eDeliveryStatusCd in (1, 4)
group by policiesid
having count(distinct eDeliveryStatusCd) = 2;
答案 1 :(得分:1)
如果该表在policiesid,eDeliveryStatusCd上是唯一的,则另一个答案有效。如果没有尝试:
select policiesid, count(*)
from (select distinct policiesid, eDeliveryStatusCd
from UserPolicyHistory uph1
where PoliciesID is not null
and eDeliveryStatusCd in (1,4))
having count(*) > 2
答案 2 :(得分:1)
使用exists()
:
select policiesid, eDeliveryStatusCd
from UserPolicyHistory as t
where PoliciesID is not null
and eDeliveryStatusCd in (1,4)
and exists (
select 1
from UserPolicyHistory as i
where i.policiesid = t.policiesid
and i.eDeliveryStatusCd in (1,4)
and i.eDeliveryStatusCd <> t.eDeliveryStatusCd
)
order by policiesid