2016-07-12 8 views
0

毎月最後の水曜日に日付の範囲内で作業する必要があります。毎月最終水曜日

私は最後の水曜日を計算するコードを持っていますが、私のcteは月を正しく歩いていないようです。

現在のコード:

2016年1月27日:

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 

eomonth

select dateadd(dd,datediff(dd,0,dateadd(mm,1,D_Date))/7*7+2,0) 

を変更することは

期待される結果のいずれかを動作するようには思えません
2016年2月24日
2016年3月30日
2016年4月27日
2016年5月25日
2016年6月29日

+0

あなたが最後の水曜日の計算コードを持っているとしたら、どこにありますか? 'WeekDay'を何も使わずにどの日がWedsかを計算する方法は? –

+0

ちょっと脇に... ** ids **は日付のために使用されました。なぜなら、その日付を使用しなければならない理由があるからです。 – SeanC

+0

@ TabAlleman、先週の水曜日のコードは別のサイトからのもので、月の最後の日を使用する必要がありました。これがEOMONTHを使用し、最初の日付を1月の最終日に設定した理由です。 @@ DATEFIRSTはデフォルトの米国設定、7または日曜日とみなすことができます – SeanC

答えて

2

ここEOMONTH()でそれを行うための方法だとDATEFROMPARTS()

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) As 
(
    Select DateFromParts(Year(@S_Date), Month(@S_Date), 1) Union All 
    Select DateAdd(Day, 1, Date) 
    From Date 
    Where Date < EOMonth(@E_Date) 
) 
Select Max(Date) As LastWednesday 
From Date 
Where DatePart(WeekDay, Date) = 4 
Group By Year(Date), Month(Date) 
Option (MaxRecursion 0) 

出力

分割せずにここで
LastWednesday 
2016-01-27 
2016-02-24 
2016-03-30 
2016-04-27 
2016-05-25 
2016-06-29 
+0

すばらしい仕事を終えました... – DineshDB

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) 
0

あなたはcalendar table ..私を必要とするには、カレンダーのテーブルを持っており、スクリーンショットの下にそれがどのように簡単な方法を示します

select * from dbo.calendar where year='2013' and wkdname='Wednesday' 
and last=1 
..使用

クエリを確認するか、各月の前水曜日を得ます

あなたの要件ごとに変更することができます上記のクエリの出力:

enter image description here

0

これはeomonthとかなり単純なソリューションです。

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 
関連する問題