私はこのクエリを持っています:
SELECT 'Submission Date' = CAST(Submitted AS DATE)
,'DayOfWeek' = DATEPART(weekday, CAST(Submitted AS DATE))
,'DayOfWeekForm' = DATENAME(dw, CAST(Submitted AS DATE))
,'Submission Count' = count(*)
,'End Column' = CASE
WHEN (DATEPART(weekday, CAST(Submitted AS DATE))) IN (
'1'
,'7'
)
THEN ('Weekend')
ELSE DATENAME(dw, CAST(Submitted AS DATE))
END
,'End Sub' = CASE
WHEN ('End Column' = 'Weekend')
THEN NULL
ELSE count(*)
END
FROM dbo.xyz
WHERE CAST(Submitted AS DATE) BETWEEN '2012-09-01'
AND '2012-09-10'
GROUP BY CAST(Submitted AS DATE)
ORDER BY CAST(Submitted AS DATE)
そして、次のように返します。
Sub Date DW DayOfWeek Sub Count End Column End Sub
--------- -- ---------- --------- ---------- -------
9/1/2012 7 Saturday 20 Weekend 20
9/4/2012 3 Tuesday 126 Tuesday 126
9/5/2012 4 Wednesday 141 Wednesday 141
9/6/2012 5 Thursday 167 Thursday 167
9/7/2012 6 Friday 132 Friday 132
9/8/2012 7 Saturday 28 Weekend 28
9/9/2012 1 Sunday 6 Weekend 6
9/10/2012 2 Monday 210 Monday 210
私の目標は、週末からの提出物を前の金曜日に追加することです。つまり、09-01、09-08、09-09 の End Sub フィールドに NULL を表示したい。そして 09-07 は 166 を表示します。