我有这个查询:
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。