0

例如

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

我需要这样做以便按周分组并显示在带有正确系列标签的图表中。

4

2 回答 2

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
于 2013-10-11T08:24:07.520 回答
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
于 2013-10-11T07:59:24.100 回答