英文:
Postgresql- Select Multiple rows dynamically from a single row
问题
我有一个名为"month frequency"(freq_mnth)的列。如果某行的频率为3,那么该记录必须显示4次,以完成一年的计划,日期计算为3个月+每条记录。
类似地,如果频率为4,那么该记录必须显示3次,日期计算为4个月+。
如果频率为12,就按原样显示记录。
我有以下表格
Id | Date | freq_mnth |
---|---|---|
1 | 2023-01-01 | 3 |
2 | 2023-01-01 | 12 |
3 | 2023-01-01 | 4 |
我需要以下结果集
Id | Date | freq_mnth |
---|---|---|
1 | 2023-01-01 | 3 |
1 | 2023-04-01 | 3 |
1 | 2023-08-01 | 3 |
1 | 2023-12-01 | 3 |
2 | 2023-01-01 | 12 |
3 | 2023-01-01 | 4 |
3 | 2023-06-01 | 4 |
3 | 2023-12-01 | 4 |
谢谢,
Ramya
英文:
I have a column called month frequency(freq_mnth). If frequency is 3 for a row, the record has to be shown 4 times to complete one year plan with the date counting 3 months + for every record
Likewise of the frequency is 4, the record has to be shown 3 times with the date counting 4 months +
And if frequency is 12, show the record as is
I have a following table
Id | Date | freq_mnth |
---|---|---|
1 | 2023-01-01 | 3 |
2 | 2023-01-01 | 12 |
3 | 2023-01-01 | 4 |
I need the following result set
Id | Date | freq_mnth |
---|---|---|
1 | 2023-01-01 | 3 |
1 | 2023-04-01 | 3 |
1 | 2023-08-01 | 3 |
1 | 2023-12-01 | 3 |
2 | 2023-01-01 | 12 |
3 | 2023-01-01 | 4 |
3 | 2023-06-01 | 4 |
3 | 2023-12-01 | 4 |
Thanks,
Ramya
答案1
得分: 0
我对你的问题没完全有解决方案,因为我不太理解确切的要求,但你可以使用generate_series来简单地扩展查询结果到一定数量的行,每行代表每个原始行。
CREATE TABLE dates
("id" int, "mdate" date, "freq_mnth" int)
;
INSERT INTO dates
("id", "mdate", "freq_mnth")
VALUES
(1, '2023-01-01', 3),
(2, '2023-01-01', 12),
(3, '2023-01-01', 4)
;
select dates.id
, dates.mdate
, (dates.mdate + (((generate_series(1,12/freq_mnth)-1)*12/freq_mnth)::text ||' month')::interval)::date as array_value_used_with_time
, generate_series(1,12/freq_mnth) as array_value
from dates
输出如下:
id mdate array_value_used_with_time array_value
1 2023-01-01 2023-01-01 1
1 2023-01-01 2023-05-01 2
1 2023-01-01 2023-09-01 3
1 2023-01-01 2024-01-01 4
2 2023-01-01 2023-01-01 1
3 2023-01-01 2023-01-01 1
3 2023-01-01 2023-04-01 2
3 2023-01-01 2023-07-01 3
这里有一个SQL Fiddle的例子。
英文:
I don't quite have a solution for you as I don't understand the exact requirements, but you can use generate_series to simply expand a query result to a certain number of rows representing each original row.
CREATE TABLE dates
("id" int, "mdate" date, "freq_mnth" int)
;
INSERT INTO dates
("id", "mdate", "freq_mnth")
VALUES
(1, '2023-01-01', 3),
(2, '2023-01-01', 12),
(3, '2023-01-01', 4)
;
select dates.id
, dates.mdate
, (dates.mdate + (((generate_series(1,12/freq_mnth)-1)*12/freq_mnth)::text ||' month')::interval)::date as array_value_used_with_time
, generate_series(1,12/freq_mnth) as array_value
from dates
Which provides output:
id mdate array_value_used_with_time array_value
1 2023-01-01 2023-01-01 1
1 2023-01-01 2023-05-01 2
1 2023-01-01 2023-09-01 3
1 2023-01-01 2024-01-01 4
2 2023-01-01 2023-01-01 1
3 2023-01-01 2023-01-01 1
3 2023-01-01 2023-04-01 2
3 2023-01-01 2023-07-01 3
Here's an example SQL Fiddle
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论