Select - TSQL中的Where子句

时间:2017-01-24 11:36:53

标签: sql sql-server sql-server-2008 tsql where-clause

我有T-SQL以下的SQL Server 2014代码。

    SELECT s.[CusNo] Supplier, 
RTRIM(CAST(s.[Customer] AS VARCHAR(50)) ) AS Name,


    sum(case when s.Date 
        BETWEEN CASE 
            WHEN    GETDATE() <= DATEADD(Day,-0,CONVERT(datetime, CONVERT(varchar(4), (year(GETDATE()))) + '-02-01'))
            THEN    DATEADD(Day,-1,CONVERT(datetime, CONVERT(varchar(4), (year(GETDATE())-1)) + '-02-01'))
            ELSE    DATEADD(Day,-1,CONVERT(datetime, CONVERT(varchar(4), (year(GETDATE()))) + '-02-01'))
        END 
        AND
        CASE 
            WHEN    GETDATE() = DATEADD(Day,-0,CONVERT(datetime, CONVERT(varchar(4), (year(GETDATE()))) + '-02-01'))
            THEN    DATEADD(Day,-1,CONVERT(datetime, CONVERT(varchar(4), (year(GETDATE()))) + '-02-01'))
            ELSE    Convert(date, dateadd(wk, datediff(wk, 0, GETDATE()) - 1, 0) + 5)
        END
           then s.SELLINC else 0 end) YrToDateActual

FROM [dbo].[CustomerReports] s
WHERE s.BRN = 1 or s.BRN = 2 or s.BRN = 3 or s.BRN = 4 or s.BRN = 5  or s.SELLINC is null or s.SELLINC = '0'
GROUP BY s.[CusNo], s.[Customer]

在上面的TSQL代码中我使用了where子句
(s.BRN = 1 or s.BRN = 2 or s.BRN = 3 or s.BRN = 4 or s.BRN = 5)

因此我得到了以下结果,

Supplier    Name               YTDA(12345)
2           A C Limited        10
13          A Co Ltd           20
24          A Worth            30

我想要达到以下答案,

YTDA(12345)= where (s.BRN = 1 or s.BRN = 2 or s.BRN = 3 or s.BRN = 4 or s.BRN = 5)
YTDA(1)= where (s.BRN = 1)

YTDA(2)= where (s.BRN = 2)

YTDA(3)= where (s.BRN = 3)

YTDA(4)= where (s.BRN = 4)

YTDA(5)= where (s.BRN = 5)

所以输出我期待这样的事情,

Supplier Name       YTDA(12345) YTDA(1) YTDA(2) YYTDA(3) YTDA(4) YTDA(5)     
    2    A C Limited    10          10      10      10       10      10
    13   A Co Ltd       20          20      20      20       20      20
    24  A W worth       30          30      30      30       30      30

请问这个T-SQL代码是什么?

2 个答案:

答案 0 :(得分:1)

将where子句放入总和中的CASE中。 例如,这应该只是s.BRN = 1

的细分
SELECT s.[CusNo] Supplier, 
   RTRIM(CAST(s.[Customer] AS VARCHAR(50)) ) AS Name,
   sum(
CASE WHEN s.BRN = 1
THEN
   case when s.Date  BETWEEN CASE 
   WHEN   GETDATE() <= DATEADD(Day,-0,CONVERT(datetime, CONVERT(varchar(4), (year(GETDATE()))) + '-02-01'))
   THEN    DATEADD(Day,-1,CONVERT(datetime, CONVERT(varchar(4),(year(GETDATE())-1)) + '-02-01'))
   ELSE    DATEADD(Day,-1,CONVERT(datetime, CONVERT(varchar(4), (year(GETDATE()))) + '-02-01'))
   END 
   AND
   CASE 
   WHEN    GETDATE() = DATEADD(Day,-0,CONVERT(datetime, CONVERT(varchar(4), (year(GETDATE()))) + '-02-01'))
   THEN    DATEADD(Day,-1,CONVERT(datetime, CONVERT(varchar(4), (year(GETDATE()))) + '-02-01'))
   ELSE    Convert(date, dateadd(wk, datediff(wk, 0, GETDATE()) - 1, 0) + 5)
   END
   then s.SELLINC else 0 end
ELSE 0  END ) YTDA(1)
   FROM [dbo].[CustomerReports] s
   WHERE s.BRN = 1 or s.BRN = 2 or s.BRN = 3 or s.BRN = 4 or s.BRN = 5  or s.SELLINC is null or s.SELLINC = '0'
   GROUP BY s.[CusNo], s.[Customer]

答案 1 :(得分:0)

也许有另一种方法来处理这种需求,

但我的如下,取决于你想要的结果。

declare @SumFun varchar (1000), 
        @Query nvarchar (max)

set @SumFun = ' sum(
       case when s.Date  BETWEEN CASE 
       WHEN   GETDATE() <= DATEADD(Day,-0,CONVERT(datetime, CONVERT(varchar(4), (year(GETDATE()))) + '-02-01'))
       THEN    DATEADD(Day,-1,CONVERT(datetime, CONVERT(varchar(4),(year(GETDATE())-1)) + '-02-01'))
       ELSE    DATEADD(Day,-1,CONVERT(datetime, CONVERT(varchar(4), (year(GETDATE()))) + '-02-01'))
       END 
       AND
       CASE 
       WHEN    GETDATE() = DATEADD(Day,-0,CONVERT(datetime, CONVERT(varchar(4), (year(GETDATE()))) + '-02-01'))
       THEN    DATEADD(Day,-1,CONVERT(datetime, CONVERT(varchar(4), (year(GETDATE()))) + '-02-01'))
       ELSE    Convert(date, dateadd(wk, datediff(wk, 0, GETDATE()) - 1, 0) + 5)
       END
       then s.SELLINC else 0 end)'

set @Query = '
SELECT s.[CusNo] Supplier, 
       RTRIM(CAST(s.[Customer] AS VARCHAR(50)) ) AS Name, ' 
        + @SumFun  +
        ' YTDA(12345) , '
                + @SumFun  +
        ' YTDA(1) , '
                + @SumFun  +
        ' YTDA(2) , '
                + @SumFun  +
        ' YTDA(3) , '
                + @SumFun  +
        ' YTDA(4) , '
                + @SumFun  +
        ' YTDA(5)  ' + 
       'FROM [dbo].[CustomerReports] s
       WHERE s.BRN = 1 or s.BRN = 2 or s.BRN = 3 or s.BRN = 4 or s.BRN = 5  or s.SELLINC is null or s.SELLINC = '0'
       GROUP BY s.[CusNo], s.[Customer] '

exec sp_executesql @Query