在sql server中使用if else(un pivot)

时间:2017-02-15 20:30:20

标签: sql sql-server tsql

我想根据产品ID只运行一个sql语句:

如果product id = 0,那么我想运行这个sql语句:

SELECT QuarterName as Quarters, QuarterValue
  FROM
    (
       SELECT month_key,
         sum(Q1)Q1, sum(Q2)Q2, sum(Q3)Q3, sum(Q4)Q4
          FROM myTable
          where line_item_id = '2548' 
            group by month_key 
            ) stu
                 UNPIVOT
                    (QuarterValue FOR QuarterName IN (Q1, Q2, Q3, Q4)
                     ) AS QTR 
                        where month_key =2016

如果产品ID<> 0然后我想运行这个sql语句:

SELECT QuarterName as Quarters, QuarterValue
  FROM
    (
       SELECT month_key,
         sum(Q1)Q1, sum(Q2)Q2, sum(Q3)Q3, sum(Q4)Q4
          FROM myTable
          where line_item_id = '2548' 
          and product_id = 'wq2745'
            group by month_key 
            ) stu
                 UNPIVOT
                    (QuarterValue FOR QuarterName IN (Q1, Q2, Q3, Q4)
                     ) AS QTR 
                        where month_key =2016

不同之处在于第一个查询不使用where子句中的product id,而第二个查询不使用。我在C#应用程序中运行这些sql查询,因此产品id值将作为参数基于一些下拉框。

2 个答案:

答案 0 :(得分:1)

使用isnull(nullif())

declare @ProductId varchar(6);
  set @ProductId = 'wq2745' 

begin;
  select 
      Quarters = QuarterName
    , QuarterValue
  from (
   select month_key
    , sum(Q1) Q1
    , sum(Q2) Q2
    , sum(Q3) Q3
    , sum(Q4) Q4
   from myTable
   where line_item_id = '2548' 
    and product_id = isnull(nullif(@ProductId,'0'),product_id)
   group by month_key
   ) stu
  unpivot(QuarterValue for QuarterName in (Q1, Q2, Q3, Q4)) as QTR
  where month_key = 2016
end;

或以这种方式使用If和两个语句:

declare @ProductId varchar(6);
  set @ProductId = 'wq2745' 

if @ProductId = '0'
begin;
  select 
      Quarters = QuarterName
    , QuarterValue
  from (
   select month_key
    , sum(Q1) Q1
    , sum(Q2) Q2
    , sum(Q3) Q3
    , sum(Q4) Q4
   from myTable
   where line_item_id = '2548'
   group by month_key
   ) stu
  unpivot(QuarterValue for QuarterName in (Q1, Q2, Q3, Q4)) as QTR
  where month_key = 2016
end;

if @ProductId != '0'
begin;
  select QuarterName as Quarters
   , QuarterValue
  from (
   select month_key
    , sum(Q1) Q1
    , sum(Q2) Q2
    , sum(Q3) Q3
    , sum(Q4) Q4
   from myTable
   where line_item_id = '2548' 
    and product_id = @ProductId
   group by month_key
   ) stu
  unpivot(QuarterValue for QuarterName in (Q1, Q2, Q3, Q4)) as QTR
  where month_key = 2016
end;

答案 1 :(得分:0)

此处不需要两个不同的查询。您可以使用案例表达式轻松处理此问题。这样的事情。

SELECT QuarterName as Quarters, QuarterValue
  FROM
    (
       SELECT month_key,
         sum(Q1)Q1, sum(Q2)Q2, sum(Q3)Q3, sum(Q4)Q4
          FROM myTable
          where line_item_id = '2548' 
          and product_id = case @ProductId when <> 0 then 'wq2745' else product_id end
            group by month_key 
            ) stu
                 UNPIVOT
                    (QuarterValue FOR QuarterName IN (Q1, Q2, Q3, Q4)
                     ) AS QTR 
                        where month_key =2016