根据日期从单行生成多行

时间:2019-06-05 07:23:01

标签: sql postgresql

我有一个数据库表,其中包含开始日期和月数。如何根据月数将其转换为多行?

我想改变这个

Source table

对此:

Result table

4 个答案:

答案 0 :(得分:1)

我们可以在此处尝试使用日历表,其中包括可能出现在预期输出中的所有月份开始日期:

with calendar as (
    select '2017-09-01'::date as dt union all
    select '2017-10-01'::date union all
    select '2017-11-01'::date union all
    select '2017-12-01'::date union all
    select '2018-01-01'::date union all
    select '2018-02-01'::date union all
    select '2018-03-01'::date union all
    select '2018-04-01'::date union all
    select '2018-05-01'::date union all
    select '2018-06-01'::date union all
    select '2018-07-01'::date union all
    select '2018-08-01'::date
)

select
    t.id as subscription_id,
    c.dt,
    t.amount_monthly
from calendar c
inner join your_table t
    on c.dt >= t.start_date and
       c.dt < t.start_date + (t.month_count::text || ' month')::interval
order by
    t.id,
    c.dt;

enter image description here

Demo

答案 1 :(得分:0)

使用Postgres中的generate_series()可以轻松完成此操作

select t.id, 
       g.dt::date,
       t.amount_monthly
from the_table t
   cross join generate_series(t.start_date, 
                              t.start_date + interval '1' month * (t.month_count - 1),  
                              interval '1' month) as g(dt);

答案 2 :(得分:0)

您可能不需要那么多子查询,但这应该可以帮助您了解如何将其分解

WITH date_minmax AS(
    SELECT
        min(start_date) as date_first,
        (max(start_date) + (month_count::text || ' months')::interval)::date AS date_last
    FROM "your_table"
    GROUP BY month_count
), series AS (
    SELECT generate_series(
        date_first,
        date_last,
        '1 month'::interval
    )::date as list_date
    FROM date_minmax
)
SELECT
    id as subscription_id,
    list_date as date,
    amount_monthly as amount
FROM series
JOIN "your_table"
    ON list_date <@ daterange(
            start_date,
            (start_date + (month_count::text || ' months')::interval)::date
    )
ORDER BY list_date

这应该达到预期的效果http://www.sqlfiddle.com/#!17/7d943/1

答案 3 :(得分:0)

好的,在PostgreSQL中很容易实现,只需使用generate_series,如下所示:

select * from month_table ;
  id  | start_date | month_count | amount | amount_monthly 
------+------------+-------------+--------+----------------
 1382 | 2017-09-01 |           3 |     38 |           1267
 1383 | 2018-02-01 |           6 |     50 |            833
(2 rows)

select                     
    id,
    generate_series(start_date,start_date + (month_count || ' month') :: interval - '1 month'::interval, '1 month'::interval)::date as date,
    amount_monthly
from
    month_table ;
  id  |    date    | amount_monthly 
------+------------+----------------
 1382 | 2017-09-01 |           1267
 1382 | 2017-10-01 |           1267
 1382 | 2017-11-01 |           1267
 1383 | 2018-02-01 |            833
 1383 | 2018-03-01 |            833
 1383 | 2018-04-01 |            833
 1383 | 2018-05-01 |            833
 1383 | 2018-06-01 |            833
 1383 | 2018-07-01 |            833
(9 rows)