Sql 如何在postgres中获得下一个业务日期?
工作日为周一至周五 如果我有一个日期时间字段计划_,如何找到下一个业务日期并在列别名中返回 我试过另一个答案,但效果不理想Sql 如何在postgres中获得下一个业务日期?,sql,postgresql,date,Sql,Postgresql,Date,工作日为周一至周五 如果我有一个日期时间字段计划_,如何找到下一个业务日期并在列别名中返回 我试过另一个答案,但效果不理想 EXTRACT(ISODOW FROM v.scheduled_for)::integer) % 7 as next_business_day, 错误: Query 1 ERROR: ERROR: syntax error at or near ")" LINE 3: EXTRACT(ISODOW FROM v.scheduled_for)::integer % 7)
EXTRACT(ISODOW FROM v.scheduled_for)::integer) % 7 as next_business_day,
错误:
Query 1 ERROR: ERROR: syntax error at or near ")"
LINE 3: EXTRACT(ISODOW FROM v.scheduled_for)::integer % 7) as next...
^
编辑:
感谢您的建议,我尝试了以下方法:
SELECT
v.id AS visit_id,
(IF extract(''dow'' from v.scheduled_for) = 0 THEN
return v.scheduled_for + 1::integer;
ELSIF extract(''dow'' from v.scheduled_for) = 6 THEN
return v.scheduled_for - 1::integer;
ELSE
return v.scheduled_for;
) as next_business_day,
'' as invoice_ref_code,
我得到的错误是:
Query 1 ERROR: ERROR: syntax error at or near ")"
LINE 1: ) as next_business_day,
^
要进行概括,您需要创建一个函数来计算从给定日期开始的下一个工作日
create or replace function utl_next_business_day(date_in date default current_date)
returns date
language sql immutable leakproof strict
as $$
with cd as (select extract(isodow from date_in)::integer d)
select case when d between 1 and 4
then date_in + 1
else date_in + 1 + (7-d)
end
from cd;
$$;
--- any single date
select current_date, utl_next_business_day();
-- over time span (short)
select gdate::date for_date, utl_next_business_day(gdate::date) next_business_day
from generate_series( current_date, current_date + 14, interval '1 day') gdate;
-- around year end over a time span
with test_date (dt) as
( values (date '2019-12-31')
, (date '2020-12-31'), (date '2021-12-31'),(date '2022-12-31')
, (date '2021-01-01'), (date '2022-01-01'),(date '2023-01-01')
)
select dt, utl_next_business_day(dt) from test_date
order by dt;
或者是@Eric的日历表建议
-- create and populate work table
create table bus_day_calendar ( bus_day date);
insert into bus_day_calendar (bus_day)
select utl_next_business_day(gdate::date)
from generate_series( date '2018-12-31', date '2023-01-01', interval '1 day') gdate
where extract(isodow from gdate)::integer not in (6,7) ;
--- Function to return next business day
create or replace function utl_next_cal_business_day(date_in date default current_date)
returns date
language sql stable leakproof strict
as $$
select min(bus_day)
from bus_day_calendar
where bus_day > date_in;
$$;
--- any single date
select current_date, utl_next_cal_business_day();
-- over time span (short)
select gdate::date for_date, utl_next_cal_business_day(gdate::date) next_business_day
from generate_series( current_date, current_date + 14, interval '1 day') gdate;
-- around year end over a time span
with test_date (dt) as
( values (date '2019-12-31')
, (date '2020-12-31'), (date '2021-12-31'),(date '2022-12-31')
, (date '2021-01-01'), (date '2022-01-01'),(date '2023-01-01')
)
select dt, utl_next_cal_business_day(dt) from test_date
order by dt;
这两种方法目前都不能处理周一至周五的非工作日,但都可以修改以实现这一目的。由于日历表只需要删除ROE,因此如果有必要,我认为这将成为更好的方法 计数
(
和)
。相同的数字?本文可能会提供您需要的代码:“工作日”数据操作最好使用在您的环境中创建和维护的日历表来实现。搜索“SQLCalendarTable”应该为您提供许多潜在的起点,以构建一个适合您业务需要的起点。