汇总每月行的创建日期和结束日期

Aggregate monthly rows created date and ended date

我需要将当前 BI 实施中的图表调整为 SQL 中的图表。此图反映了收到的请求数量,每个请求都有 3 个与此查询相关的字段:ID、创建日期和结束日期。

图形看起来像这样 https://i.stack.imgur.com/NRIjr.png:

+----+--------------+-------------+
| ID |  CREATE_DATE |   END_DATE  |
+----+--------------+-------------+
|    |              |             |
| 1  |  2022-01-01  |  2022-02-10 |
|    |              |             |
| 2  |  2022-01-03  |  2022-03-01 |
|    |              |             |
| 3  |  2022-02-01  |  2022-04-01 |
|    |              |             |
| 4  |  2022-03-01  |  null       |
+----+--------------+-------------+

所以对于这个特定的例子,我们会有这样的东西:

所以对于每个月,我都想要那个特定月份的活动请求(那些结束日期在那个特定月份之后或者为空的请求)和那个月完成的请求(这个可能会被拆分到另一个查询,当然)我试过这个查询,但是当然,它没有考虑到特定月份结束的请求,只给我累计总和

编辑:我忘了提到其中一个要求是图表的开始和结束日期可能由用户设置。所以也许我想查看从 2022 年 4 月到 2020 年 4 月的月份,并查看 2 年的行为!

 WITH cte AS ( SELECT
        date_trunc('month',
        r.date_init) AS mon,
        count(r.id) AS mon_sum
    FROM
        "FOLLOWUP"."CAT_REQUEST" r
    GROUP  BY
        1     )  SELECT
        to_char(mon,
        'YYYY-mm') AS mon_text,
        COALESCE(sum(c.mon_sum) 
                     OVER (ORDER BY mon),
                 0) AS running_sum
    FROM
        generate_series('2022-01-01', '2023-12-25',
        interval '1 month') mon
    LEFT   JOIN
        cte c USING (mon)
    ORDER  BY
        mon

我使用一些不同的业务逻辑为您编写了查询。但是,结果将与您需要的结果相同。示例查询:

with month_list as (
    select 1 as id,  'Yanuary' as mname     union all
    select 2 as id,  'Febriary' as mname    union all
    select 3 as id,  'Marth' as mname       union all   
    select 4 as id,  'April' as mname       union all   
    select 5 as id,  'May' as mname         union all   
    select 6 as id,  'June' as mname        union all   
    select 7 as id,  'Jule' as mname        union all   
    select 8 as id,  'August' as mname      union all   
    select 9 as id,  'September' as mname   union all   
    select 10 as id, 'October' as mname     union all   
    select 11 as id, 'November' as mname    union all   
    select 12 as id, 'December' as mname
), 
test_table as (
    select 
        id, 
        create_date, 
        end_date, 
        extract(month from create_date) as month1, 
        extract(month from end_date) as month2 
    from 
        your_table
)
select 
    t1.mname, 
    count(*) as "actived"
from 
    month_list t1 
inner  join 
    test_table t2 on (t1.id >= t2.month1) and (t1.id < t2.month2)
group by 
    t1.id, t1.mname
order by 
    t1.id


/* --- Result: 

mname       actived
--------------------
 Yanuary     2
 Febriary    2
 Marth       1

*/

PostgreSQL 有许多日期和时间函数和类型。 我为你写了一些样本: 例如,在我的示例函数中 now() 我们选择的日期。

-- get previos 12 month from date (return timestampt)
select now() - '12 month'::interval as newdate 
-- Return:
2021-04-03 18:22:48.344 +0400

-- if you need only date, you can cast this to date 
select (now() - '12 month'::interval)::date as newdate 
-- Return:
2021-04-03


-- generate data from previous 12 month to selected date increase by month: 
SELECT t1.datelist::date 
from generate_series
     (
        now()-'12 month'::interval, 
        now(),
        '1 month'
     ) 
AS t1(datelist)
-- Return:
2021-04-03
2021-05-03
2021-06-03
2021-07-03
2021-08-03
2021-09-03
2021-10-03
2021-11-03
2021-12-03
2022-01-03
2022-02-03
2022-03-03
2022-04-03


-- generate data from previous 12 month to selected date increase by month with extracting month names and year: 
-- this sample may be as you needed.
SELECT 
    extract(year from t1.datelist) as "year", 
    TO_CHAR(t1.datelist, 'Month') as "month", 
    trim(TO_CHAR(t1.datelist, 'Month')) || '-' || trim(to_char(t1.datelist, 'yyyy')) as "formatted_date"
from generate_series
     (
        now()-'12 month'::interval, 
        now(),
        '1 month'
     ) 
AS t1(datelist)
-- Return: 

year    month       formatted_date
------------------------------------
2021    April       April-2021
2021    May         May-2021
2021    June        June-2021
2021    July        July-2021
2021    August      August-2021
2021    September   September-2021
2021    October     October-2021
2021    November    November-2021
2021    December    December-2021
2022    January     January-2022
2022    February    February-2022
2022    March       March-2022
2022    April       April-2022