0

我有一张桌子:

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 YEARLY、MONTHLY 和 WEEKLY 的 ContentId 数量,并以特定方式格式化它们:

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 表(或表变量)并继续输入年/月/周数据,但如果可能的话,我想使用更有效的方法?

4

1 回答 1

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
于 2012-11-19T23:58:43.707 回答