将列聚合为年/月/周

时间:2012-11-19 17:35:10

标签: sql-server-2008 tsql

我有一张表:

Table A
-------
SubscriberId int
ContentId int
SendDate DateTime

Sample Data
------------
1            190159      2012-11-07 00:00:00.000
1            190160      2012-11-07 00:00:00.000
1            190160      2012-11-07 00:00:00.000
1            190160      2012-11-07 00:00:00.000
1            190160      2012-11-07 00:00:00.000
1            190160      2012-11-07 00:00:00.000
1            190160      2012-11-07 00:00:00.000
1            190160      2012-11-19 00:00:00.000
2            190160      2012-11-19 00:00:00.000
2            190160      2012-10-19 00:00:00.000
2            190160      2012-10-01 00:00:00.000
2            190160      2011-10-01 00:00:00.000
2            190160      2011-10-01 00:00:00.000
2            190160      2011-10-01 00:00:00.000
2            190160      2011-10-19 00:00:00.000
2            190160      2011-10-18 00:00:00.000
2            190160      2011-10-17 00:00:00.000
2            190161      2011-10-20 00:00:00.000
2            190161      2011-10-01 01:01:01.000

上表是一个平面表,没有标准化(我打算保持这种方式)

我需要的输出是,对于每个SubscriberId,我需要计算Senddate每年,每月和每周的ContentIds数量,并以特定方式格式化它们:

Subscriber ID    YEARLYValidData
1                190159,1|190160,7
2                190160,9|190161,2  

以上结果是部分结果,即我得到的结果是为YEARLY数据工作,但是在第二列我需要MONTHLYValiddata(即来自GetDate()30天内的内容)我还需要WEEKLYValidData(即GetDate()中7天内的内容

以下查询给出了我想要的结果,但每个结果都是分开的:

DECLARE NumberOfDays int = 365
SELECT  MAIN.SubscriberId,
            STUFF((
                SELECT '|' + 
                (CAST(SUB.ContentId AS nvarchar(10)) + ',' + 
                 CAST(Count(ContentId) as nvarchar(100))) 
                FROM TABLEA SUB
                Where
                SUB.SubscriberId = MAIN.SubscriberId
                GROUP BY ContentId
                FOR XML PATH('')
                ), 1, 1, '' )
            AS [Result]
            FROM  TABLEA MAIN
            WHERE 
            DATEADD(DAY, DATEDIFF(DAY, 0, SendDate), 0) >= 
            DATEADD(dd, DATEDIFF(dd, 0, getdate()), 0) - @NumberOfDays

如何在单个Select语句中获得以下结果:

Subscriber ID    YEARLYValidData       MONTHLYValidData         WEEKLYValidData
1                190159,1|190160,7     190159,1|190160,7        190159,1|190160,7       
2                190160,9|190161,2     190160,9|190161,2        190160,1

我知道我可以使用TEMP表(或表变量)并继续输入每年/每月/每周数据但我想在可能的情况下使用更高效的东西吗?

1 个答案:

答案 0 :(得分:1)

我不太明白,但这可能会让你在路上走得更远,即使它不是最终的答案:

SELECT  C.Interval,
        MAIN.SubscriberId,
        STUFF((
            SELECT '|' + 
            (CAST(SUB.ContentId AS nvarchar(10)) + ',' + 
             CAST(Count(ContentId) as nvarchar(100))) 
            FROM TABLEA SUB
            Where
            SUB.SubscriberId = MAIN.SubscriberId
            GROUP BY ContentId
            FOR XML PATH('')
            ), 1, 1, '' )
        AS [Result]
        FROM  TABLEA MAIN
        CROSS JOIN
        (    SELECT 356 As Days, 'Year' As Interval
             UNION ALL 
             SELECT 30 Days, 'Month' As Interval
             UNION ALL 
             SELECT 7 Days, 'Week' As Interval
        ) C
        WHERE 
        DATEADD(DAY, DATEDIFF(DAY, 0, SendDate), 0) >= 
        DATEADD(dd, DATEDIFF(dd, 0, getdate()), 0) - C.Days