通过使用SQL服务器,我试图过滤掉与每个策略号相关联的行,当“本地货币的高级”总和为零,然后最后一行在“当前状态”中具有“取消”柱。
Workno# MasterPolicyNumber EffectiveDate CurrentStatus PremiumLocalCurrency
16-05-01-035210-01 42-PRP-302562-01 2016-05-12 Bound 61438
16-05-01-035210-02 42-PRP-302562-01 2016-05-12 Cancellation -61438
以下是我使用的SQL查询
select
[Workno#],
[MasterPolicyNumber],
[EffectiveDate],
[CurrentStatus],
[PremiumLocalCurrency]
from IT.dbo.View_Rater_Of_Record
where [MasterPolicyNumber] = '42-PRP-302562-01'
order
by [Workno#]
但是我希望通过过滤掉两行来将结果作为零记录,因为总和加起来为零,最后一行将“取消”作为状态。提前致谢。
我也尝试过使用Having子句,但是我没有得到结果。下面的行过滤第二行,因为它小于1.
Having SUM(CAST([Premium in Local Currency]as float))>1
答案 0 :(得分:1)
假设SQL服务器:
with Canc as
(
select MasterPolicyNumber, EffectiveDate, CurrentStatus, PremiumLocalCurrency,
row_number() over(partition by MasterPolicyNumber order by Workno# desc) as Row_ord
from View_Rater_Of_Record
)
select C1.MasterPolicyNumber, sum(C2.PremiumLocalCurrency) as TotPLC
from Canc C1
inner join Canc C2
on C1.MasterPolicyNumber = C2.MasterPolicyNumber
where C1.Row_Ord = 1
and C1.CurrentStatus = 'Cancellation'
group by C1.MasterPolicyNumber
having sum(C2.PremiumLocalCurrency) <> 0
编辑:忘了基本的加入...