我有表数据表
ID Task Response by Created
1 mark 2015-03-01 3:02:44 PM
2 harry 2015-03-02 3:06:22 PM
3 alex 2015-03-01 3:14:02 PM
4 jim 2015-03-30 3:18:33 PM
5 jim 2015-03-30 3:24:49 PM
6 alex 2015-03-30 3:34:35 PM
现在,我希望按周计算任务响应总数,并输出
week mark harry alex jim
1 1 1 2 2
任务响应的名称是n号
答案 0 :(得分:1)
我根据您的输出给出了结果集,但是当你有3月份时他们确实不能假设一周的结果(他们会给出10和14)
declare @t table (Id int,Task varchar(20),created date)
insert into @t (Id,Task,created)values (1,'mark','2015-01-01 3:02:44 PM'),
(2,'harry','2015-01-01 3:02:44 PM'),
(3,'alex','2015-01-01 3:02:44 PM'),
(4,'jim','2015-01-01 3:02:44 PM'),
(5,'jim','2015-01-01 3:02:44 PM'),
(6,'alex','2015-01-01 3:02:44 PM')
;with CTE as(
SELECT [mark],[harry],[alex],[jim],created FROM (
Select [mark],[harry],[alex],[jim],created from (
Select A.Id,A.Task,A.created,COUNT(A.RN)OVER(PARTITION BY TASK )R from (select ID,Task,created,ROW_NUMBER()OVER(PARTITION BY task order by task desc)RN from @t
GROUP BY ID,Task,created)A)B
PIVOT(MAX(R)FOR Task IN ([mark],[harry],[alex],[jim]))P)V
GROUP BY [mark],[harry],[alex],[jim],created)
select DATEPART(week,created)Week,MIN(mark)[mark],MIN(harry)[harry],MIN(alex)[alex],MIN(jim)[jim] FROM CTE
GROUP BY created