我需要在表中找到具有0溢价的策略,但是如果具有相同策略编号的另一个策略具有溢价则不需要。这是我的伪代码:
select * from tblPolicies where premium = 0
(but not if anther record has the same policyNumber and premium <> 0)
我知道这很可怕,但我迷路了,这是我能想到的最好的!
答案 0 :(得分:1)
可能的查询1:
SELECT M.* FROM tblPolicies AS M
WHERE premium = 0 AND policyNumber NOT IN
(
SELECT S.policyNumber
FROM tblPolicies AS S
WHERE S.policyNumber = M.policyNumber
AND S.premium <> 0
)
可能的查询2:
SELECT SA.* FROM
(
SELECT M.* FROM tblPolicies AS M
WHERE premium = 0
) AS SA
INNER JOIN tblPolicies AS SB
ON SA.policyNumber = SB.policyNumber
WHERE SB.premium <> 0
可能的查询3:
SELECT * FROM tblPolicies AS SA
INNER JOIN tblPolicies AS SB
ON SA.policyNumber = SB.policyNumber
WHERE SA.premium = 0 AND SB.premium <> 0
答案 1 :(得分:1)
select p_no from
(select p_no, sum(premium) as tot_prem from policy group by p_no) as pol
where tot_prem>0;
p_no-&GT;的 premium_no 强>
policy-&gt; 政策表