1

假设我们有一张桌子:

╔═════════════════════════════════════╗
║ 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()by 间隔?

4

4 回答 4

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上的工作示例。

于 2013-09-02T19:13:53.313 回答
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]
于 2013-09-02T19:15:23.263 回答
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 示例
My SQL Fiddle 示例

于 2013-09-02T19:29:40.063 回答
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);
于 2013-09-03T22:58:09.147 回答