postgresql创建每月的天数表

时间:2017-02-17 20:11:33

标签: sql postgresql date

我正在尝试编写一个脚本,该脚本返回月份列表,其中包含该月份的天数。它引用了这个表

 CREATE TABLE generic.time_series_only (measurementdatetime TIMESTAMP WITHOUT TIME ZONE NOT NULL)

这只是一个按时间顺序排列的时间序列(在连接不同地方的数据表时很有用,但是你想要一个完整的时间序列作为你的输出,也许有更聪明的方法来做到这一点但我没有&# 39; t发现它。)

SELECT date_part('year'::text, time_series_only.measurementdatetime) AS
    measyear,
         date_part('month'::text, time_series_only.measurementdatetime) AS
           measmonth,
         date_trunc('month'::text, time_series_only.measurementdatetime) +
           '1 mon'::interval - date_trunc('month'::text,
           time_series_only.measurementdatetime) AS days_in_month
  FROM generic.time_series_only
  GROUP BY date_part('year'::text, time_series_only.measurementdatetime),
           date_part('month'::text, time_series_only.measurementdatetime)
  ORDER BY date_part('year'::text, time_series_only.measurementdatetime),
           date_part('month'::text, time_series_only.measurementdatetime);

但是我收到了这个错误:

  ERROR:  column "time_series_only.measurementdatetime" must appear in the GROUP BY clause or be used in an aggregate function

我无法将此列放在GROUP BY子句中,因为我会在time_series_only表中获得每个条目的结果,而我无法找到获得相同内容的方法结果使用聚合函数?任何建议都非常欢迎: - )

4 个答案:

答案 0 :(得分:2)

你没有使用generate_series?..就像这里:

vao=# with pre as (select generate_series('2016-01-01','2017-03-31','1 day'::interval) g) select distinct
extract('year' from g), extract('month' from g), count(1) over (partition by date_trunc('month',g)) from pre order by 1,2;
 date_part | date_part | count
-----------+-----------+-------
      2016 |         1 |    31
      2016 |         2 |    29
      2016 |         3 |    31
      2016 |         4 |    30
      2016 |         5 |    31
      2016 |         6 |    30
      2016 |         7 |    31
      2016 |         8 |    31
      2016 |         9 |    30
      2016 |        10 |    31
      2016 |        11 |    30
      2016 |        12 |    31
      2017 |         1 |    31
      2017 |         2 |    28
      2017 |         3 |    31
(15 rows)

答案 1 :(得分:1)

使用distinct on一对(年,月)。您可以使用函数time_series_only替换generate_series()表,例如:

select distinct on (date_part('year', d), date_part('month', d))
    date_part('year', d) as year, 
    date_part('month', d) as month, 
    date_part('day', d) as days_in_month
from 
    generate_series('2016-01-01'::date, '2016-12-31'::date, '1d'::interval) d
order by 1, 2, 3 desc;

 year | month | days_in_month 
------+-------+---------------
 2016 |     1 |            31
 2016 |     2 |            29
 2016 |     3 |            31
 2016 |     4 |            30
 2016 |     5 |            31
 2016 |     6 |            30
 2016 |     7 |            31
 2016 |     8 |            31
 2016 |     9 |            30
 2016 |    10 |            31
 2016 |    11 |            30
 2016 |    12 |            31
(12 rows)

答案 2 :(得分:1)

这个性能更好,因为它只生成每个月的最后一天,因此不需要聚合:

select
    date_part('year', d) as year, 
    date_part('month', d) as month, 
    date_part('day', d) as days_in_month
from 
    generate_series('2016-01-01'::date, '2016-12-01', '1 month') gs(gsd)
    cross join lateral
    (select gsd + interval '1 month - 1 day') d(d)
order by 1, 2;

 year | month | days_in_month 
------+-------+---------------
 2016 |     1 |            31
 2016 |     2 |            29
 2016 |     3 |            31
 2016 |     4 |            30
 2016 |     5 |            31
 2016 |     6 |            30
 2016 |     7 |            31
 2016 |     8 |            31
 2016 |     9 |            30
 2016 |    10 |            31
 2016 |    11 |            30
 2016 |    12 |            31

答案 3 :(得分:0)

另一种变体,使用CTE具有更好的可读性,恕我直言(此示例在current_date的日历月之后生成月份和下三个完整月份的数据)

WITH series AS (                                                                                                                                                   
  SELECT generate_series ( 
    date_trunc ('month', date_trunc('day', now()) + interval '1 month'), 
    date_trunc('day', now() + interval '4 months'), '1d'::interval
  ) AS day                                                                                                                                                                               )                                                                                                                                                                                 SELECT DISTINCT ON (date_part('year', series.day),     date_part('month', series.day))
  date_part('year', series.day) as year,
  date_part('month', series.day) as month,
  date_part('day', series.day) as days_in_month
FROM series
ORDER BY 1, 2, 3 desc LIMIT 3;

 year | month | days_in_month 
------+-------+---------------
 2021 |     1 |            31
 2021 |     2 |            28
 2021 |     3 |            31