返回空结果行时的情况

时间:2017-10-13 10:24:59

标签: sql-server reporting-services

对于SSRS报告,我根据用户输入通过使用Case来回复3年不同年份的收入。

DECLARE @Year as int
Set @Year = 2018
DECLARE @Period as int
Set @Period = 1

SELECT Account
, Customer
, CustomerDescription
, Article
, ArticleDescription
, Sum (Case when Year = @Year -2 then NNNRevenue Else 0 End) as RevenueP2
, Sum (Case when Year = @Year -1 then NNNRevenue Else 0 End) as RevenuePY
, Sum (Case when Year = @Year then NNNRevenue Else 0 End) as RevenueCY
 FROM [PP_Test].[dbo].[History]
 WHERE Line = 'Inkoopartikel'
And Period = @Period
 GROUP BY Account, customer, CustomerDescription, Article, ArticleDescription

示例结果:

Account  Customer  CustomerDescription  Article  ArticleDescription  RevenueP2   RevenuePY  RevenueCY 
A        A         CustomerA            1234     Transport           0           0          0
A        A         CustomerA            2345     Croissant           12,15       0          14578,87

现在,当我有一篇2015年出售的文章时,它在我的3个收入列中显示为0。我想消除那些null / 0行。

我通过添加以下

找到了mysql的解决方案
 HAVING RevenueP2 <> 0

然而,这不适用于Server management studio,因为我无法引用新列。

有没有办法解决这个问题?

2 个答案:

答案 0 :(得分:2)

你可以通过两种方式做到这一点

  1. 将聚合函数放入HAVING子句

    HAVING Sum (Case when Year = @Year -2 then NNNRevenue Else 0 End) <> 0
    
  2. 使用派生表概念

    SELECT * 
    FROM(
      Paste your query here wihtout where clause
    )m
    Where RevenueP2 <> 0
    

答案 1 :(得分:0)

having Sum (Case when Year = @Year -2 then NNNRevenue Else 0 End) <> 0