我需要在表格中查找保费为 0 的保单,但如果另一个具有相同保单编号的保单具有保费,则不需要。这是我的伪代码:
select * from tblPolicies where premium = 0
(but not if anther record has the same policyNumber and premium <> 0)
我知道这很糟糕,但我迷路了,这是我能想到的最好的!
可能的问题 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
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-> premium_no
策略->策略表