【问题标题】:If statement calculationif语句计算
【发布时间】:2022-01-19 22:38:47
【问题描述】:

只有当“类型列”中的数据=总发生损失、多付损失、救助和代位求偿时,我才需要对“金额”列中的数据求和。你知道我可以在下面的代码中使用什么 if 语句来完成这个吗?

select        total.*     
from          (
select        p.policyNum,
                            p.stateID,
                            p.effectiveDate as date,
                            p.premiumTotal,
              a.claimFreeDiscount,
                            a.homeownerDiscount,
                            a.policyZip,
                            mb.claimnumber,
                            **if .....**
                            sum(mb.amount)     as totalclm 
                            
                from aggressive.dbo.Policy                                                  as p
              inner join Aggressive.dbo.Auto                                          as a  
                on p.policyID = a.policyID
                      left join MicrosoftPowerBI.dbo.ClaimTransactionDetail                as mb
                on p.policyid = mb.policyid
                where p.stateID = 74 and
                p.companyID = 10 and
                p.status =1
                                                                                                                                             
                group by p.policynum,
                         p.stateID,
                               p.effectiveDate,
                                 p.premiumTotal,
                 a.claimFreeDiscount,
                             a.homeownerDiscount,
                               a.policyZip,
                                 mb.claimnumber)                                                
                                                                                                   as total
          order by total.totalclm desc

【问题讨论】:

  • 该代码看起来像 SQL - 请在您正在使用的数据库的问题中添加一个标签。

标签: sql if-statement sum


【解决方案1】:

SQL 没有 IF 语句,它有 CASE 语句。

如果类型列在列表中,则只需使用 CASE 语句创建具有所需值的列,否则它的值为零 - 然后对该列求和

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-15
    • 2021-11-24
    • 1970-01-01
    • 1970-01-01
    • 2018-07-01
    • 1970-01-01
    相关资源
    最近更新 更多