2017-10-13 97 views
1

對於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行。

我已通過添加以下

HAVING RevenueP2 <> 0 

然而,這並不爲Server Management Studio中工作,我不能引用新列中找到針對MySQL的解決方案。

有沒有辦法解決這個問題?

回答

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 
    
0
having Sum (Case when Year = @Year -2 then NNNRevenue Else 0 End) <> 0