나는 당신이 2
내가 그것을 상자 밖으로 작동합니다 있는지 확실하지 않습니다 코멘트 번호
비슷한
WITH your_query AS
(
SELECT *
FROM
(
SELECT
ISNULL(FORMAT((DATEADD(wk, DATEDIFF(wk, 0, DATEADD(dd,-1,J.JobDate)), 1) +5), 'dd/MM/yyyy'), 'Completed to Date') as Weekend
,U.Name + ' ' + U.Surname AS Technician
,(CASE WHEN U.Active = 1 THEN 'Active' ELSE 'Inactive' END) AS Active
,COUNT(CASE WHEN SFS.FormId IN (66,69,68,79,94,72) THEN 1 ELSE NULL END) AS Total
FROM Jobs J
LEFT Join dbo.USERS_LIST_ALL() U ON
U.id = J.UserId
LEFT JOIN SimplyFormsSubmitted SFS ON
SFS.FormsSubmittedStatus = 'SUBMITTED'
AND SFS.JobId = J.id
AND SFS.DateSubmitted IS NOT NULL
WHERE
U.GroupId = 2 -- User Technician
AND J.DepartmentId = 132 -- Department
AND SFS.FormId IN (66,69,68,79,94,72)
GROUP BY
U.Active
,U.Name + ' ' + U.Surname
,ROLLUP(FORMAT((DATEADD(wk, DATEDIFF(wk, 0, DATEADD(dd,-1,J.JobDate)), 1) +5), 'dd/MM/yyyy'))
) AS RefTable
pivot (SUM(Total) for Weekend in (
[02/10/2016],
[09/10/2016],
[16/10/2016],
[23/10/2016],
[30/10/2016],
[06/11/2016],
[13/11/2016],
[20/11/2016],
[27/11/2016],
[04/12/2016],
[11/12/2016],
[18/12/2016],
[25/12/2016],
[01/01/2017],
[08/01/2017],
[15/01/2017],
[22/01/2017],
[29/01/2017],
[05/02/2017],
[12/02/2017],
[19/02/2017],
[26/02/2017],
[05/03/2017],
[12/03/2017],
[19/03/2017],
[Completed to Date]
)) as Answer
)
SELECT * FROM your_query
UNION ALL
SELECT
'TOTAL',
null,
sum(isnull([02/10/2016],0)),
sum(isnull([09/10/2016],0)),
sum(isnull([16/10/2016],0)),
sum(isnull([23/10/2016],0)),
sum(isnull([30/10/2016],0)),
sum(isnull([06/11/2016],0)),
sum(isnull([13/11/2016],0)),
sum(isnull([20/11/2016],0)),
sum(isnull([27/11/2016],0)),
sum(isnull([04/12/2016],0)),
sum(isnull([11/12/2016],0)),
sum(isnull([18/12/2016],0)),
sum(isnull([25/12/2016],0)),
sum(isnull([01/01/2017],0)),
sum(isnull([08/01/2017],0)),
sum(isnull([15/01/2017],0)),
sum(isnull([22/01/2017],0)),
sum(isnull([29/01/2017],0)),
sum(isnull([05/02/2017],0)),
sum(isnull([12/02/2017],0)),
sum(isnull([19/02/2017],0)),
sum(isnull([26/02/2017],0)),
sum(isnull([05/03/2017],0)),
sum(isnull([12/03/2017],0)),
sum(isnull([19/03/2017],0)),
sum(isnull([Completed to Date],0))
FROM your_query
UPD을 필요로 생각하지만, 당신은 코드를 수정할 수 있습니다 귀하의 필요에 따라
WITH step1 AS
(
SELECT
DATEADD(wk, DATEDIFF(wk, 0, DATEADD(dd,-1,J.JobDate)), 1)+5 as WeekendDate
,U.Name + ' ' + U.Surname AS Technician
,(CASE WHEN U.Active = 1 THEN 'Active' ELSE 'Inactive' END) AS Active
,COUNT(CASE WHEN SFS.FormId IN (66,69,68,79,94,72) THEN 1 ELSE NULL END) AS Total
FROM Jobs J
LEFT Join dbo.USERS_LIST_ALL() U ON
U.id = J.UserId
LEFT JOIN SimplyFormsSubmitted SFS ON
SFS.FormsSubmittedStatus = 'SUBMITTED'
AND SFS.JobId = J.id
AND SFS.DateSubmitted IS NOT NULL
WHERE
U.GroupId = 2 -- User Technician
AND J.DepartmentId = 132 -- Department
AND SFS.FormId IN (66,69,68,79,94,72)
GROUP BY
U.Active
,U.Name + ' ' + U.Surname
,ROLLUP(DATEADD(wk, DATEDIFF(wk, 0, DATEADD(dd,-1,J.JobDate)), 1)+5)
),
RefTable AS
(
SELECT
ISNULL(FORMAT(WeekendDate, 'dd/MM/yyyy'), 'Completed to Date') as Weekend
,Technician
,Active
,avg(Total) over (
partition by Technician
order by WeekendDate desc
rows between current row and 3 following) as Avg4
,Total
FROM step1
),
your_query AS
(
SELECT *
FROM RefTable
pivot (SUM(Total) for Weekend in (
[02/10/2016],
[09/10/2016],
[16/10/2016],
[23/10/2016],
[30/10/2016],
[06/11/2016],
[13/11/2016],
[20/11/2016],
[27/11/2016],
[04/12/2016],
[11/12/2016],
[18/12/2016],
[25/12/2016],
[01/01/2017],
[08/01/2017],
[15/01/2017],
[22/01/2017],
[29/01/2017],
[05/02/2017],
[12/02/2017],
[19/02/2017],
[26/02/2017],
[05/03/2017],
[12/03/2017],
[19/03/2017],
[Completed to Date]
)) as Answer
)
SELECT * FROM your_query
UNION ALL
SELECT
'TOTAL',
null,
null,
sum(isnull([02/10/2016],0)),
sum(isnull([09/10/2016],0)),
sum(isnull([16/10/2016],0)),
sum(isnull([23/10/2016],0)),
sum(isnull([30/10/2016],0)),
sum(isnull([06/11/2016],0)),
sum(isnull([13/11/2016],0)),
sum(isnull([20/11/2016],0)),
sum(isnull([27/11/2016],0)),
sum(isnull([04/12/2016],0)),
sum(isnull([11/12/2016],0)),
sum(isnull([18/12/2016],0)),
sum(isnull([25/12/2016],0)),
sum(isnull([01/01/2017],0)),
sum(isnull([08/01/2017],0)),
sum(isnull([15/01/2017],0)),
sum(isnull([22/01/2017],0)),
sum(isnull([29/01/2017],0)),
sum(isnull([05/02/2017],0)),
sum(isnull([12/02/2017],0)),
sum(isnull([19/02/2017],0)),
sum(isnull([26/02/2017],0)),
sum(isnull([05/03/2017],0)),
sum(isnull([12/03/2017],0)),
sum(isnull([19/03/2017],0)),
sum(isnull([Completed to Date],0))
FROM your_query
감사합니다. 나는 이렇게 대답 해 주셔서 감사합니다. –
위의 쿼리에서 [Completed to Date] 열 앞에 기술자가 마지막 4 주말 평균 완료 작업을 원합니다. –
@FaysalMemon 답변을 변경했습니다. –