我需要能够每月显示所有周数,无论该周是否有几个小时。
改变这个:
2.00 2 July
2.25 3 July
7.25 5 July
对此:
0.00 1 July
2.00 2 July
2.25 3 July
0.00 4 July
7.25 5 July
以下是我正在使用的当前查询:
SELECT SUM(te.Hours_Bill) AS HoursBilled
, DATEPART(dd, DATEDIFF(dd, 0, te.Date_Start) / 7 * 7 ) / 7 + 1 AS HoursWeekNumber
, DATENAME(mm, te.Date_Start) AS HoursMonthName
FROM AGR_Header ah
INNER
JOIN Time_Entry te
ON te.Agr_Header_RecID = ah.AGR_Header_RecID
INNER
JOIN Company co
ON co.Company_RecID = ah.Company_RecID
WHERE (te.Date_Start >= DATEADD(mm, DATEDIFF(mm, 0, GETDATE()) -6, 0) AND te.Date_Start <= DATEADD(mm, DATEDIFF(mm, -1, GETDATE()) -1, -1))
GROUP
BY DATEPART(dd, DATEDIFF(dd, 0, te.Date_Start) / 7 * 7 ) / 7 + 1
, DATENAME(mm, te.Date_Start)
, DATEPART(mm, te.Date_Start)
ORDER
BY DATEPART(mm, te.Date_Start) ASC
, DATEPART(dd, DATEDIFF(dd, 0, te.Date_Start) / 7 * 7 ) / 7 + 1 ASC
默认情况下,我想显示一个月的所有周数,如果一周没有结果,则显示0。
答案 0 :(得分:0)
这可能对您有所帮助
CREATE TABLE #t
(
hr NUMERIC(22, 6),
weekno INT,
mon VARCHAR(20)
)
INSERT INTO #t
VALUES
(2.00 , 2 , 'October'),
(2.25 , 3 , 'October'),
(7.25 , 5 , 'October')
WITH dates AS (
SELECT 1 id,datename(month,(getdate())) mon
UNION ALL
SELECT id+1, mon FROM dates WHERE id<5
)
SELECT Isnull(hr, 0) hrs,
id AS weekno,
d.mon AS [month]
FROM #t t
RIGHT JOIN dates d
ON t.mon = d.mon
AND t.weekno = d.id
输出
hrs weekno month
0.000000 1 October
2.000000 2 October
2.250000 3 October
0.000000 4 October
7.250000 5 October