如何获得去年的第一个 6 个月,即(1-26 周)和最后 6 个月(26-52 周)以及使用 postgresql 从当前日期获得最后 5 个星期
How to get the 1st 6months i.e.(1-26weeks) and last 6months (26-52 weeks) from last year & also get last 5weeks from current date using postgresql
如何获取去年的前 6 个月,即(1-26 周)和最后 6 个月(26-52 周)以及如何使用 postgresql 获取当前日期的最后 5 周。
像下面的table结构
Id Title Description current_week_number current_year
-----------------------------------------------------
123 abc descr 48 2021
456 def descr1 45 2020
基于我尝试获取数据的周数和年份。
有人可以帮忙吗?
谢谢
--records from 1st half of this year, based on week number
select Id, Title, Description, current_week_number, current_year
from your_table
where current_year=extract('year' from now())::int
and current_week_number<=26;
--records from 2nd half of this year, based on week number
select Id, Title, Description, current_week_number, current_year
from your_table
where current_year=extract('year' from now())::int
and current_week_number>26;
关于问题的原始形式,获取实际周数:
使用 generate_series(date,date,interval)
and extract('field' from date)
.
with first_26_weeks_of_last_year as
( select extract('week' from weeks) weeks
from generate_series(
make_date( extract('year' from 'today'::timestamp-'1 year'::interval)::int,
1,
1
),
make_date( extract('year' from 'today'::timestamp-'1 year'::interval)::int,
1,
1
)+'25 weeks'::interval,
'1 week'::interval) weeks),
last_6_months_of_last_year as
( select extract('week' from weeks) weeks
from generate_series(
make_date( extract('year' from 'today'::timestamp-'1 year'::interval)::int,
12,
31
),
make_date( extract('year' from 'today'::timestamp-'1 year'::interval)::int,
12,
31
)-'6 months'::interval,
'1 week'::interval) weeks),
five_weeks_from_this_week as
( select extract('week' from weeks) weeks
from generate_series(
'today'::date,
'today'::date+'4 weeks'::interval,
'1 week'::interval) weeks)
select 'first_26_weeks_of_last_year',a.weeks
from first_26_weeks_of_last_year a
union all
select 'last_6_months_of_last_year',a.weeks
from last_6_months_of_last_year a
union all
select 'five_weeks_from_this_week',a.weeks
from five_weeks_from_this_week a;
查询最近 6 个月到现在的 table :
SELECT *
FROM your_table
WHERE (current_year || '0101') :: date + interval '7 days' * current_week >= Now() - interval '6 months'
AND (current_year || '0101') :: date + interval '7 days' * current_week <= Now()
查询最近12个月到最近6个月的table:
SELECT *
FROM your_table
WHERE (current_year || '0101') :: date + interval '7 days' * current_week <= Now() - interval '6 months'
AND (current_year || '0101') :: date + interval '7 days' * current_week >= Now() - interval '12 months'
查询当年前6个月的table:
SELECT *
FROM your_table
WHERE (current_year || '0101') :: date + interval '7 days' * current_week >= (extract(year from Now()) || '0101') :: date
AND (current_year || '0101') :: date + interval '7 days' * current_week <= (extract(year from Now()) || '0101') :: date + interval '6 months'
查询当年最后6个月的table:
SELECT *
FROM your_table
WHERE (current_year || '0101') :: date + interval '7 days' * current_week >= (extract(year from Now()) || '0101') :: date + interval '6 months'
AND (current_year || '0101') :: date + interval '7 days' * current_week <= (extract(year from Now()) || '0101') :: date + interval '12 months'
如何获取去年的前 6 个月,即(1-26 周)和最后 6 个月(26-52 周)以及如何使用 postgresql 获取当前日期的最后 5 周。
像下面的table结构
Id Title Description current_week_number current_year
-----------------------------------------------------
123 abc descr 48 2021
456 def descr1 45 2020
基于我尝试获取数据的周数和年份。
有人可以帮忙吗?
谢谢
--records from 1st half of this year, based on week number
select Id, Title, Description, current_week_number, current_year
from your_table
where current_year=extract('year' from now())::int
and current_week_number<=26;
--records from 2nd half of this year, based on week number
select Id, Title, Description, current_week_number, current_year
from your_table
where current_year=extract('year' from now())::int
and current_week_number>26;
关于问题的原始形式,获取实际周数:
使用 generate_series(date,date,interval)
and extract('field' from date)
.
with first_26_weeks_of_last_year as
( select extract('week' from weeks) weeks
from generate_series(
make_date( extract('year' from 'today'::timestamp-'1 year'::interval)::int,
1,
1
),
make_date( extract('year' from 'today'::timestamp-'1 year'::interval)::int,
1,
1
)+'25 weeks'::interval,
'1 week'::interval) weeks),
last_6_months_of_last_year as
( select extract('week' from weeks) weeks
from generate_series(
make_date( extract('year' from 'today'::timestamp-'1 year'::interval)::int,
12,
31
),
make_date( extract('year' from 'today'::timestamp-'1 year'::interval)::int,
12,
31
)-'6 months'::interval,
'1 week'::interval) weeks),
five_weeks_from_this_week as
( select extract('week' from weeks) weeks
from generate_series(
'today'::date,
'today'::date+'4 weeks'::interval,
'1 week'::interval) weeks)
select 'first_26_weeks_of_last_year',a.weeks
from first_26_weeks_of_last_year a
union all
select 'last_6_months_of_last_year',a.weeks
from last_6_months_of_last_year a
union all
select 'five_weeks_from_this_week',a.weeks
from five_weeks_from_this_week a;
查询最近 6 个月到现在的 table :
SELECT *
FROM your_table
WHERE (current_year || '0101') :: date + interval '7 days' * current_week >= Now() - interval '6 months'
AND (current_year || '0101') :: date + interval '7 days' * current_week <= Now()
查询最近12个月到最近6个月的table:
SELECT *
FROM your_table
WHERE (current_year || '0101') :: date + interval '7 days' * current_week <= Now() - interval '6 months'
AND (current_year || '0101') :: date + interval '7 days' * current_week >= Now() - interval '12 months'
查询当年前6个月的table:
SELECT *
FROM your_table
WHERE (current_year || '0101') :: date + interval '7 days' * current_week >= (extract(year from Now()) || '0101') :: date
AND (current_year || '0101') :: date + interval '7 days' * current_week <= (extract(year from Now()) || '0101') :: date + interval '6 months'
查询当年最后6个月的table:
SELECT *
FROM your_table
WHERE (current_year || '0101') :: date + interval '7 days' * current_week >= (extract(year from Now()) || '0101') :: date + interval '6 months'
AND (current_year || '0101') :: date + interval '7 days' * current_week <= (extract(year from Now()) || '0101') :: date + interval '12 months'