如何在postgresql中获取给定日期的周日总数
发布于 2011-02-17 23:07:22
给定日期的周日总数只能是0或1。
但是,如果您想要给定日期范围内的周日数量,那么最好的选择是日历表。为了找出今年2月份有多少个星期天,我只需
select count(*)
from calendar
where cal_date between '2011-02-01' and '2011-02-28' and
day_of_week = 'Sun';或
select count(*)
from calendar
where year_of_date = 2011 and
month_of_year = 2 and
day_of_week = 'Sun';这是一个基本的日程表,您可以从它开始。我还包含了一个PostgreSQL函数来填充日程表。我还没有在8.3中测试过这一点,但我非常确定我没有使用8.3不支持的任何特性。
注意,"dow“部分假设你的日子是用英语写的。但您可以轻松地编辑这些部分以匹配任何语言。(我想。但我可能对“容易”一词的理解是错误的。)
-- Table: calendar
-- DROP TABLE calendar;
CREATE TABLE calendar
(
cal_date date NOT NULL,
year_of_date integer NOT NULL,
month_of_year integer NOT NULL,
day_of_month integer NOT NULL,
day_of_week character(3) NOT NULL,
CONSTRAINT calendar_pkey PRIMARY KEY (cal_date),
CONSTRAINT calendar_check CHECK (year_of_date::double precision = date_part('year'::text, cal_date)),
CONSTRAINT calendar_check1 CHECK (month_of_year::double precision = date_part('month'::text, cal_date)),
CONSTRAINT calendar_check2 CHECK (day_of_month::double precision = date_part('day'::text, cal_date)),
CONSTRAINT calendar_check3 CHECK (day_of_week::text =
CASE
WHEN date_part('dow'::text, cal_date) = 0::double precision THEN 'Sun'::text
WHEN date_part('dow'::text, cal_date) = 1::double precision THEN 'Mon'::text
WHEN date_part('dow'::text, cal_date) = 2::double precision THEN 'Tue'::text
WHEN date_part('dow'::text, cal_date) = 3::double precision THEN 'Wed'::text
WHEN date_part('dow'::text, cal_date) = 4::double precision THEN 'Thu'::text
WHEN date_part('dow'::text, cal_date) = 5::double precision THEN 'Fri'::text
WHEN date_part('dow'::text, cal_date) = 6::double precision THEN 'Sat'::text
ELSE NULL::text
END)
)
WITH (
OIDS=FALSE
);
ALTER TABLE calendar OWNER TO postgres;
-- Index: calendar_day_of_month
-- DROP INDEX calendar_day_of_month;
CREATE INDEX calendar_day_of_month
ON calendar
USING btree
(day_of_month);
-- Index: calendar_day_of_week
-- DROP INDEX calendar_day_of_week;
CREATE INDEX calendar_day_of_week
ON calendar
USING btree
(day_of_week);
-- Index: calendar_month_of_year
-- DROP INDEX calendar_month_of_year;
CREATE INDEX calendar_month_of_year
ON calendar
USING btree
(month_of_year);
-- Index: calendar_year_of_date
-- DROP INDEX calendar_year_of_date;
CREATE INDEX calendar_year_of_date
ON calendar
USING btree
(year_of_date);以及用于填充表的基本函数。我也没有在8.3中测试过这个。
-- Function: insert_range_into_calendar(date, date)
-- DROP FUNCTION insert_range_into_calendar(date, date);
CREATE OR REPLACE FUNCTION insert_range_into_calendar(from_date date, to_date date)
RETURNS void AS
$BODY$
DECLARE
this_date date := from_date;
BEGIN
while (this_date <= to_date) LOOP
INSERT INTO calendar (cal_date, year_of_date, month_of_year, day_of_month, day_of_week)
VALUES (this_date, extract(year from this_date), extract(month from this_date), extract(day from this_date),
case when extract(dow from this_date) = 0 then 'Sun'
when extract(dow from this_date) = 1 then 'Mon'
when extract(dow from this_date) = 2 then 'Tue'
when extract(dow from this_date) = 3 then 'Wed'
when extract(dow from this_date) = 4 then 'Thu'
when extract(dow from this_date) = 5 then 'Fri'
when extract(dow from this_date) = 6 then 'Sat'
end);
this_date = this_date + interval '1 day';
end loop;
END;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;发布于 2011-02-17 23:03:47
你需要提取:
SELECT
EXTRACT(DOW FROM DATE '2011-02-16') = 0; -- 0 is Sunday这可能会导致对或错,可能是星期天,也可能不是。我不知道您所说的“总数”是什么意思,因为它始终是0(日期不是星期天)或1(给定数据是星期天)。
编辑:像这样吗?
SELECT
COUNT(*)
FROM
generate_series(timestamp '2011-01-01', '2011-03-01', '1 day') AS g(mydate)
WHERE
EXTRACT(DOW FROM mydate) = 0;https://stackoverflow.com/questions/5030546
复制相似问题