【问题标题】:Apply multiple "Where" clauses in one Query SQL SERVER在一个查询 SQL SERVER 中应用多个“Where”子句
【发布时间】:2023-03-13 16:07:01
【问题描述】:

我想要

我有这个问题:

select 

SUM(fact.cheques) as TotalChequeAmount,
SUM(fact.revenusIntermediationToDate + fact.cheques ) as YTD ,
SUM(fact.revenusIntermediationToDate + fact.cheques ) as YTDN1,
SUM(revenusIntermediationToDate) as TotalRevenuIntermediationToDate,

acc.beCompanyCode as Codecompany,
acc.Country as Pays,
acc.Name as Name,
acc.AccountTypeSec as AccountType,
acc.Segmentation as Segmentation,
acc.City as City,
acc.OfficialGroup as OfficialGroup , 
acc.ActualYearBudget as Budget2016,
acc.SegmentationResSec as SegmentSecurities

from dbo.Fact_Profit_And_Loss fact
left outer join dbo.Dim_Accounts acc  on ( acc.Accounts_TechKey = fact.FK_Account)
left outer join dbo.DimTemps time1 on (time1.Temps_PK = fact.FK_Time)
where YEAR(time1.Date) = 2016
group by acc.beCompanyCode, acc.Country , acc.Name , acc.AccountTypeSec , acc.Segmentation ,
acc.City , acc.OfficialGroup , acc.ActualYearBudget, acc.SegmentationResSec 

问题是我想为 YTD 列应用过滤器 Year = 2016 YTDN1 列的另一个过滤器 Year = 2015。

是否有可能在一个 Sql 查询中为两列定义两个 where 子句?

TotalChequeAmount   YTD         YTDN1               Codecompany
0.00                6541.2826   6541.2826           2513
0.00                0.00        0.00                2541
0.00                7350.9433   7350.9433           2547

【问题讨论】:

    标签: sql sql-server database where-clause


    【解决方案1】:

    使用条件聚合

    select SUM(fact.cheques) as TotalChequeAmount,
           SUM(CASE 
                  WHEN YEAR(time1.Date) = 2016 
                     THEN fact.revenusIntermediationToDate + fact.cheques 
                  ELSE 0 
               END) as YTD ,
           SUM(CASE 
                  WHEN YEAR(time1.Date) = 2015 
                     THEN fact.revenusIntermediationToDate + fact.cheques 
                  ELSE 0 
               END) as YTDN1,
           SUM(revenusIntermediationToDate) as TotalRevenuIntermediationToDate,    
           acc.beCompanyCode as Codecompany,
           acc.Country as Pays,
           acc.Name as Name,
           acc.AccountTypeSec as AccountType,
           acc.Segmentation as Segmentation,
           acc.City as City,
           acc.OfficialGroup as OfficialGroup , 
           acc.ActualYearBudget as Budget2016,
           acc.SegmentationResSec as SegmentSecurities    
    from dbo.Fact_Profit_And_Loss fact
    left outer join dbo.Dim_Accounts acc  
       on ( acc.Accounts_TechKey = fact.FK_Account)
    left outer join dbo.DimTemps time1 on (time1.Temps_PK = fact.FK_Time)
    where YEAR(time1.Date) IN (2015, 2016)
    group by acc.beCompanyCode, acc.Country , 
             acc.Name , acc.AccountTypeSec , 
             acc.Segmentation , acc.City , 
             acc.OfficialGroup , acc.ActualYearBudget, 
             acc.SegmentationResSec 
    

    上述查询的YTD字段返回2016年的结果,而YTDN1字段返回2015年的结果。SUM的其他字段返回这两年的总和。

    【讨论】:

      猜你喜欢
      • 2015-09-04
      • 1970-01-01
      • 1970-01-01
      • 2016-02-27
      • 1970-01-01
      • 1970-01-01
      • 2019-05-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多