週の範囲が曜日に基づく 2 つの日付で週の範囲を生成できるソリューションを探しています。
例:
date1 = 2011-12-17 date2 = 2012-05-16
結果:
week1 2011-12-17 to 2011-12-17 -- since it the first date false on a saturday
week2 2011-12-18 to 2011-12-24
...
last week 2012-05-13 to 2012-05-16
私は PostgreSQL で SQL を書きました:
SELECT
time_series.cur
,case (6 - date_part('dow', cur)::int) = 0
when true then cur
else case time_series.cur + (6 - date_part('dow',cur)::int) < current_date
when true then time_series.cur + (6 - date_part('dow', cur)::int)
else current_date
end
end
as nxt
FROM
(select generate_series(current_date - 151
, current_date, '1 day')::date AS cur) time_series
where cur < current_date
and case (current_date - 151 != cur and cur != current_date)
when true then date_part('dow', cur)::int = 0
else true
end
これを行うためのよりクリーンな方法はありますか?