2017-10-13 12 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 

サンプル結果:私は2015年に販売された記事を持っているとき

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 

は、今では私の3つの収益列に0と表示されます。それらのnull/0行を削除したいと思います。

私は新しい列を参照することはできませんので、これはしかし、サーバー管理スタジオでは動作しません、次の

HAVING RevenueP2 <> 0 

を追加することによって、mysqlのためのソリューションを見つけました。

これを解決する方法はありますか?

答えて

2

あなたは、派生テーブルの概念をUSING句

HAVING Sum (Case when Year = @Year -2 then NNNRevenue Else 0 End) <> 0 
  • を有する点で集計関数を入れて二つの方法

    1. によってこれを行うことができます

      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 
    
    関連する問題