postgresql创建每月天数表

postgresql创建每月天数表,sql,postgresql,date,Sql,Postgresql,Date,我正在尝试编写一个脚本,它返回一个月列表,其中包含该月的天数。它引用了这个表 CREATE TABLE generic.time_series_only (measurementdatetime TIMESTAMP WITHOUT TIME ZONE NOT NULL) 这只是一个按时间顺序排列的时间序列(在连接不同位置有间隙的数据表时非常有用,但是你想要一个完整的时间序列作为你的输出,也许有一种更聪明的方法可以做到这一点,但我还没有找到) 但我得到了这个错误: ERROR: colu

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

 CREATE TABLE generic.time_series_only (measurementdatetime TIMESTAMP WITHOUT TIME ZONE NOT NULL)
这只是一个按时间顺序排列的时间序列(在连接不同位置有间隙的数据表时非常有用,但是你想要一个完整的时间序列作为你的输出,也许有一种更聪明的方法可以做到这一点,但我还没有找到)

但我得到了这个错误:

  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表中每个条目的结果,并且我无法找到使用聚合函数获得相同结果的方法?欢迎您提供任何建议:-)

您没有使用generate\u 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)

对一对(年、月)使用不同的
。您可以使用函数
generate\u series()
替换
time\u series\u only
表格,例如:

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)

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

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

另一种变体,使用CTE提高可读性,IMHO(此示例生成当前日期日历月之后的整整三个月的月份和数据)

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
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