Sql 每个月的最后一个星期三
我需要在一系列日期内计算每个月的最后一个星期三 我有计算上周三的代码,但我的cte似乎没有正确地一步一步地走过这几个月 当前代码:Sql 每个月的最后一个星期三,sql,sql-server,sql-server-2012,Sql,Sql Server,Sql Server 2012,我需要在一系列日期内计算每个月的最后一个星期三 我有计算上周三的代码,但我的cte似乎没有正确地一步一步地走过这几个月 当前代码: declare @S_Date date declare @E_Date date set @S_Date='2016-01-31' --eomonth of first month set @E_Date='2016-06-15' --ides of last month ( can actually be any day that is
declare @S_Date date
declare @E_Date date
set @S_Date='2016-01-31' --eomonth of first month
set @E_Date='2016-06-15' --ides of last month ( can actually be any day that is
--not equal to or after the last Wednesday of the month)
;with LW(D_Date) as
(
select dateadd(dd,datediff(dd,0,@S_Date)/7*7+2,0)
union all
select dateadd(dd,datediff(dd,0,eomonth(D_Date,1))/7*7+2,0)
from LW
where D_date<@E_Date
)
select d_date
from LW
似乎也不起作用
预期成果:
2016-01-27
2016-02-24
2016-03-30
2016-04-27
2016-05-25
2016-06-29
以下是一种使用和的方法:
以下是一种使用和的方法:
这里有一个没有分区的稍微不同的方法。。。结果应相同:
declare @S_Date date
declare @E_Date date
set @S_Date='2016-01-31' --eomonth of first month
set @E_Date='2016-06-15' --ides of last month ( can actually be any day that is
--not equal to or after the last Wednesday of the month)
;With Date (Date, WD) As
(
Select DateFromParts(Year(@S_Date), Month(@S_Date), 1), DatePart(WeekDay, DateFromParts(Year(@S_Date), Month(@S_Date), 1)) AS wd
Union All
Select DateAdd(Day, 1, Date), DatePart(WeekDay, DateAdd(Day, 1, Date)) AS wd
From Date
Where Date < EOMonth(@E_Date)
)
Select Max(Date) As LastWednesday
From Date
Where wd = 4
group by MONTH(Date)
Option (MaxRecursion 0)
这里有一个没有分区的稍微不同的方法。。。结果应相同:
declare @S_Date date
declare @E_Date date
set @S_Date='2016-01-31' --eomonth of first month
set @E_Date='2016-06-15' --ides of last month ( can actually be any day that is
--not equal to or after the last Wednesday of the month)
;With Date (Date, WD) As
(
Select DateFromParts(Year(@S_Date), Month(@S_Date), 1), DatePart(WeekDay, DateFromParts(Year(@S_Date), Month(@S_Date), 1)) AS wd
Union All
Select DateAdd(Day, 1, Date), DatePart(WeekDay, DateAdd(Day, 1, Date)) AS wd
From Date
Where Date < EOMonth(@E_Date)
)
Select Max(Date) As LastWednesday
From Date
Where wd = 4
group by MONTH(Date)
Option (MaxRecursion 0)
你需要一个…我有一个日历表,下面的屏幕截图显示了每个月的最后一个星期三检查或获取是多么简单
使用的查询:
以上查询的输出,可根据需要修改:
你需要一个…我有一个日历表,下面的屏幕截图显示了每个月的最后一个星期三检查或获取是多么简单
使用的查询:
以上查询的输出,可根据需要修改:
这是一个相当简单的解决方案
这是一个相当简单的解决方案
当你说你有上周三的计算代码时,你在哪里有?你怎么计算不使用工作日就可以算出哪一天是星期三。。。ides用于日期是因为它很可爱,而不是因为任何必须使用该日期的压倒一切的原因。@TabAlleman,上周三的代码来自另一个站点,需要使用月的最后一天,这就是为什么我使用EOMONTH,并将初始日期设置为1月的最后一天@@DATEFIRST可以被认为是默认的美国设置,7或SundayOk,我猜代码是基于0天的工作日,这是一个已知的数量。当你说你有计算上周三的代码时,你在哪里有它?你怎么计算不使用工作日就可以算出哪一天是星期三。。。ides用于日期是因为它很可爱,而不是因为任何必须使用该日期的压倒一切的原因。@TabAlleman,上周三的代码来自另一个站点,需要使用月的最后一天,这就是为什么我使用EOMONTH,并将初始日期设置为1月的最后一天@@DATEFIRST可以被认为是默认的美国设置,7或SundayOk,我猜代码是基于0天的工作日,这是一个已知的数量。
LastWednesday
2016-01-27
2016-02-24
2016-03-30
2016-04-27
2016-05-25
2016-06-29
declare @S_Date date
declare @E_Date date
set @S_Date='2016-01-31' --eomonth of first month
set @E_Date='2016-06-15' --ides of last month ( can actually be any day that is
--not equal to or after the last Wednesday of the month)
;With Date (Date, WD) As
(
Select DateFromParts(Year(@S_Date), Month(@S_Date), 1), DatePart(WeekDay, DateFromParts(Year(@S_Date), Month(@S_Date), 1)) AS wd
Union All
Select DateAdd(Day, 1, Date), DatePart(WeekDay, DateAdd(Day, 1, Date)) AS wd
From Date
Where Date < EOMonth(@E_Date)
)
Select Max(Date) As LastWednesday
From Date
Where wd = 4
group by MONTH(Date)
Option (MaxRecursion 0)
select * from dbo.calendar where year='2013' and wkdname='Wednesday'
and last=1
declare @s_date date='2016-01-01',@e_date date='2016-12-31' --date range
;with eom_tbl as (--CTE
--anchor from start date
select EOMONTH(@s_date,0) eom,datepart(weekday,EOMONTH(@s_date,0)) dw
union all
--recursive query
select EOMONTH(eom,1) eom,datepart(weekday,EOMONTH(eom,1)) dw
from eom_tbl where eom<@e_date
)
select eom,dw,
--calculate last Wednesday (4)
case when dw<4 then dateadd(dd,-3-dw,eom)
else dateadd(dd,4-dw,eom) end lastWed,
--extra check
datepart(dw,case when dw<4 then dateadd(dd,-3-dw,eom)
else dateadd(dd,4-dw,eom) end ) ddw
from eom_tbl