e.g。
Date | Name
1/1/13 | John
1/3/13 | Mary
1/5/13 | Joe
1/8/13 | Mary
1/9/13 | Bob
使用如下所示的存储过程在结果记录集中创建其他列“周范围”:
Date | Name | Week Range
1/1/13 | John | 12/31/2012 to 01/06/13
1/3/13 | Mary | 12/31/2012 to 01/06/13
1/5/13 | Joev | 12/31/2012 to 01/06/13
1/8/13 | Mary | 01/07/2013 to 01/13/13
1/9/13 | Bob | 01/07/2013 to 01/13/13
我需要这样做才能按周分组并在图表中显示正确的系列标签。
答案 0 :(得分:0)
SELECT [Date],
[Name],
CONVERT(VARCHAR(10),DATEADD(day,1-DATEPART(weekday,[Date]),[Date]),3)
+ ' to '
+ CONVERT(VARCHAR(10),DATEADD(day,7,DATEADD(day,1-DATEPART(weekday,[Date]),[Date])),3)
FROM Table
答案 1 :(得分:0)
请尝试以下查询。我使用BirthDate作为日期列。
SELECT BirthDate,Name,
CONVERT(VARCHAR(10), DATEADD(dd, -(DATEPART(dw, BirthDate)-1), BirthDate)) + ' to ' +
CONVERT(VARCHAR(10),DATEADD(dd, 7-(DATEPART(dw, BirthDate)), BirthDate)) WeekRange
FROM Employee
group BY BirthDate, DATEADD(dd, -(DATEPART(dw, BirthDate)-1), BirthDate),DATEADD(dd, 7-(DATEPART(dw, BirthDate)), BirthDate),FirstName
ORDER BY BirthDate