Sql 基于日期从一行生成多行
我有一个带有开始日期和月份数的数据库表。如何根据月份数将其转换为多行 我想改变这个 为此:Sql 基于日期从一行生成多行,sql,postgresql,Sql,Postgresql,我有一个带有开始日期和月份数的数据库表。如何根据月份数将其转换为多行 我想改变这个 为此: 我们可以尝试在此处使用日历表,其中包括可能出现在预期输出中的所有可能的月初日期: 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-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;
在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);
您可能不需要这么多子查询,但这应该有助于您理解如何分解它
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
这应该会达到预期的结果好的,在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)
无法打开图片..这里的大多数人都希望示例表数据和预期结果为格式化文本,而不是图像。尝试使用标记表。这不起作用。请参阅有关格式的。或是通过?当你编辑你的问题时。@a_horse_和_no_的名字很好,它实际上并没有说任何关于表格的事情。