为了获得工作日的计数,您需要使用函数知道该月的第一天( ) 的日期。之后,您需要使用函数获取特定月份( ) 的天数,有一个关于此的wiki 页面。最后,您可以使用日期和数字的天数,不包括使用函数的周末天数。start_date
date_trunc()
month_last_day
extract()
generate_series()
start_date
month_last_day
date_part()
CREATE OR REPLACE FUNCTION extract_month_business_days(d DATE, count_remaining BOOLEAN)
RETURNS INTEGER AS $$
DECLARE
start_date DATE;
month_last_day INTEGER;
result INTEGER;
BEGIN
IF count_remaining THEN
start_date = d;
ELSE
start_date = date_trunc('month',d);
END IF;
month_last_day = extract(DAY FROM date_trunc('month',d) + INTERVAL '1 MONTH - 1 day');
SELECT count(*) INTO result FROM generate_series(0,(month_last_day - extract(DAY FROM start_date))::INTEGER) day
WHERE date_part('dow', start_date + day) NOT IN (0,6);
RETURN result;
END;
$$ LANGUAGE plpgsql;
结果:
WITH t(dates) AS ( VALUES
('2016-02-18'::DATE),
('2016-03-18'::DATE),
('2016-04-18'::DATE),
('2016-05-18'::DATE)
)
SELECT
to_char(dates,'Month YY') AS month,
extract_month_business_days(dates,FALSE) AS number_business_days,
extract_month_business_days(dates,TRUE) AS remaining_business_days
FROM t;
month | number_business_days | remaining_business_days
--------------+----------------------+-------------------------
February 16 | 21 | 8
March 16 | 23 | 10
April 16 | 21 | 10
May 16 | 22 | 10
(4 rows)
更新 - 红移版
正如@John指出的,generate_series()
在 AWS Redshift 中不可用,函数定义如下:
CREATE OR REPLACE FUNCTION extract_month_business_days(d DATE, count_remaining BOOLEAN)
RETURNS INTEGER AS $$
DECLARE
start_date DATE;
month_last_day INTEGER;
result INTEGER;
i INTEGER;
BEGIN
result = 0;
IF count_remaining THEN
start_date = d;
ELSE
start_date = date_trunc('month',d);
END IF;
month_last_day = extract(DAY FROM date_trunc('month',d) + INTERVAL '1 MONTH - 1 day');
result = 0;
FOR i IN 0..(month_last_day - extract(DAY FROM start_date))::INTEGER LOOP
IF (date_part('dow', start_date + i) NOT IN (0,6)) THEN
result = result + 1;
END IF;
END LOOP;
RETURN result;
END;
$$ LANGUAGE plpgsql;