6,129
社区成员
发帖
与我相关
我的任务
分享
SET DATEFIRST 1
--测试数据
if not object_id(N'Tempdb..#T') is null
drop table #T
Go
Create table #T([时间] Date,[平台] int,[播放量] int)
Insert #T
select '2017-6-1',1,20 union all
select '2017-6-1',2,30 union all
select '2017-6-1',3,20 union all
select '2017-6-1',4,50 union all
select '2017-6-2',1,20 union all
select '2017-6-2',2,30 union all
select '2017-6-2',4,50 union all
select '2017-6-3',1,20 union all
select '2017-6-3',2,30 union all
select '2017-6-3',3,20 union all
select '2017-6-4',1,20 union all
select '2017-6-4',2,30 union all
select '2017-6-4',3,20 union all
select '2017-6-4',4,50 union all
select '2017-6-9',1,90 union all
select '2017-6-9',4,100
Go
--测试数据结束
;WITH cte AS (
SELECT DATEPART(WEEK, [时间]) AS [时间] ,
[平台] ,
MAX([播放量]) AS [播放量]
FROM #T
GROUP BY DATEPART(WEEK, [时间]) ,
[平台]
),ctea AS (
SELECT a.时间 ,
b.平台 ,
MAX(b.播放量) AS 播放量
FROM cte a
JOIN cte b ON a.时间 >= b.时间
GROUP BY a.时间 ,
b.平台
)
SELECT 时间 ,
SUM(播放量) AS 播放量
FROM ctea
GROUP BY 时间
ORDER BY 时间
SET DATEFIRST 1
--测试数据
if not object_id(N'Tempdb..#T') is null
drop table #T
Go
Create table #T([时间] Date,[平台] int,[播放量] int)
Insert #T
select '2017-6-1',1,20 union all
select '2017-6-1',2,30 union all
select '2017-6-1',3,20 union all
select '2017-6-1',4,50 union all
select '2017-6-2',1,20 union all
select '2017-6-2',2,30 union all
select '2017-6-2',4,50 union all
select '2017-6-3',1,20 union all
select '2017-6-3',2,30 union all
select '2017-6-3',3,20 union all
select '2017-6-4',1,20 union all
select '2017-6-4',2,30 union all
select '2017-6-4',3,20 union all
select '2017-6-4',4,50 union all
select '2017-6-9',4,50
Go
--测试数据结束
;WITH cte AS (
SELECT DATEPART(WEEK, [时间]) AS [时间] ,
SUM([播放量]) AS [播放量]
FROM #T
GROUP BY DATEPART(WEEK, [时间])
)
SELECT a.时间 ,
SUM(b.播放量) AS 播放量
FROM cte a
JOIN cte b ON a.时间 >= b.时间
GROUP BY a.时间