这就是我现在使用的,它返回beginDate和endDate之间的天数
date( ' $(@endDate)') - date('$(@beginDate)') as weekNumber
如何让它返回两者之间的周数?
答案 0 :(得分:0)
最简单的方法是将天数除以7,尽管这不会考虑一周的开始日。
你可以改用它:
extract (week from date '$(@endDate)') -extract (week from date '$(@startDate)')
将使用iso8601周数。但要注意跨越多年!
答案 1 :(得分:0)
版本1:
/*
Design:
Week begins on day of start date
Options:
1) Count only whole weeks
2) Count partial weeks on the right side (end date side)
*/
select
sum(case when ('2013-02-08'::date - ind::date) >= 7 then 1 else 0 end) as whole_weeks,
count(*) as partial_right
from
generate_series('2013-01-01'::date /*dow=2*/,'2013-02-05'::date /*dow=2*/,'7 days') ind
第2版:
/*
Design:
Week begins on specific day of week (5 chosen in this example)
Options:
1) Count only whole weeks
2) Count partial weeks on the right side (end date side)
3) Count partial weeks on the left side (start date side)
4) Count partial weeks on both sides
*/
select
sum(case when days = 7 then 1 else 0 end) as whole_weeks,
sum(case when days = 7 or max_ind = week_start then 1 else 0 end) as partial_right,
sum(case when days = 7 or week_start < min_ind then 1 else 0 end) as partial_left,
count(*) as partial_both_sides
from
(
select
ind - (case when dow < bow then dow + 7 - bow else dow - bow end)::int as week_start,
count(*) as days,
min(ind) as min_ind,
max(ind) as max_ind
from
(select
ind::date as ind,
extract(isodow from ind) as dow,
5::int as bow
from
generate_series('2013-01-01'::date /*dow=2*/,'2013-02-08'::date /*dow=5*/,'1 day') ind
) inp
group by
week_start
) t