通过DISTINCT间隔(日期)SELECT SUM()

时间:2013-09-02 19:08:44

标签: sql sql-server tsql

假设我们有一张表:

╔═════════════════════════════════════╗
║ Name   Date                   Value ║
╠═════════════════════════════════════╣
║ John   2013-01-01 10:20:00    10    ║
║ John   2013-01-01 12:20:11    20    ║
║ Mark   2013-01-01 11:44:10    10    ║
║ Mark   2013-01-02 12:00:00    20    ║
║ Mark   2013-01-03 15:20:00    20    ║
║ Tim    2013-01-01 15:11:12    5     ║
║ Tim    2013-01-03 18:44:44    10    ║
║ Tim    2013-01-03 20:11:00    15    ║
╚═════════════════════════════════════╝

使用单个SELECT查询,输出:

╔════════════════════════════════════════════════╗
║ Name    2013-01-01    2013-01-02    2013-01-03 ║
╠════════════════════════════════════════════════╣
║ John    30            0             0          ║
║ Mark    10            20            20         ║
║ Tim     5             0             25         ║
╚════════════════════════════════════════════════╝
  • 3天是固定的(2013-01-01,2013-01-02,2013-01-02)。

如何在一个SELECT中执行此操作?我试过SUM(DISTINCT)但没有成功。我无法弄清楚逻辑。

它必须只是GROUP BY Name(我认为),但我如何按间隔计算SUM()

4 个答案:

答案 0 :(得分:3)

试试这个:

SELECT NAME, 
       SUM(CASE 
             WHEN CAST(DATE AS DATE) = '2013-01-01' THEN VALUE 
             ELSE 0 
           END) [2013-01-01], 
       SUM(CASE 
             WHEN CAST(DATE AS DATE) = '2013-01-02' THEN VALUE 
             ELSE 0 
           END) [2013-01-02], 
       SUM(CASE 
             WHEN CAST(DATE AS DATE) = '2013-01-03' THEN VALUE 
             ELSE 0 
           END) [2013-01-03] 
FROM   TABLE1 
GROUP  BY NAME 

查看SQL Fiddle上的工作示例。

答案 1 :(得分:3)

如果日期已修复:

SELECT  [Name],
        SUM(CASE WHEN [Date] >= '20130101' 
            AND [Date] < '20130102' THEN Value END) [2013-01-01],
        SUM(CASE WHEN [Date] >= '20130102' 
            AND [Date] < '20130103' THEN Value END) [2013-01-02],
        SUM(CASE WHEN [Date] >= '20130103' 
            AND [Date] < '20130104' THEN Value END) [2013-01-03]
FROM YourTable
GROUP BY [Name]

答案 2 :(得分:2)

SELECT NAME, 
    ISNULL([2013-01-01],0) [2013-01-01], 
    ISNULL([2013-01-02],0) [2013-01-02], 
    ISNULL([2013-01-03],0) [2013-01-03]
FROM (
    SELECT NAME, CONVERT(DATE, [Date]) [Date], SUM([Value]) [Value]
    FROM   TABLE1
    GROUP  BY NAME, CONVERT(DATE, [Date])
  ) src
PIVOT (
   SUM([Value])
   FOR [Date]
   IN ( [2013-01-01], [2013-01-02], [2013-01-03])
  ) pvt

日期列可能必须以不同方式指定,具体取决于它们的打印方式。

SQL Authority Pivot Example
My SQL Fiddle Example

答案 3 :(得分:0)

试试这个:

DECLARE @cols AS NVARCHAR(MAX)
       ,@query AS NVARCHAR(MAX)

SELECT @cols = STUFF((
                     SELECT ',' + QUOTENAME(CAST(DATE as DATE))
                     FROM (
                           SELECT DISTINCT CAST(DATE as DATE) DATE FROM #YourTable
                           ) tbl
                     ORDER BY DATE
                     FOR XML PATH('')
                           ,TYPE
                     ).value('.', 'NVARCHAR(MAX)'), 1, 1, '');

SET @query = 
       'WITH CTE (
    NAME
    ,DATE
    ,VALUE
    )
AS (
    SELECT NAME
        ,CAST(DATE AS DATE)
        ,Cast(Value AS INT)
    FROM #YourTable
    )
SELECT NAME
    ,' + @cols + '
FROM CTE
PIVOT(SUM(Value) FOR DATE IN (' + @cols + ')) PVT;'

EXECUTE (@query);