我们在数据库中使用查找表来分组数周,数月和数年。可悲的是,制作它的那个人早就不见了,日历在年底就用尽了!所以我真的需要找到一种方法来添加它!它的设置如下:
我在sql(MS2008)中表现不是很好,说实话,我已经把它搞砸了,我们不能设置任何新东西,因为一切都围绕着这个!
答案 0 :(得分:1)
你可以这样做,但我不明白为什么你的日历从三月开始?
declare @Date_Start date, @Date_End date
select @Date_Start = '20130101'
select @Date_End = '20131231'
;with
CTE_Dates as (
select @Date_Start as [Date]
union all
select dateadd(dd, 1, [Date])
from CTE_Dates
where [Date] < @Date_End
),
CTE_Calendar as (
select
[Date],
datename(dw, [Date]) as [Day],
datepart(ww, [Date]) as [Week],
datepart(mm, [Date]) as [MonthID],
dateadd(mm, datediff(mm, 0, getdate()), 0) as [Month],
datepart(yy, [Date]) as [YearID],
dateadd(yy, datediff(yy, 0, getdate()), 0) as [Year],
datepart(qq, [Date]) as [QuarterID],
dateadd(qq, datediff(qq, 0, getdate()), 0) as [Quarter]
from CTE_Dates
)
select
row_number() over (order by [Date]) + @Start_ID - 1 as ID,
*
from CTE_Calendar
option (maxrecursion 0)