英文:
In oracle SQL am trying to print dates in one column which is between two date column and grouping based on ID
问题
下面是一个示例,我有类似的数据,希望将两个日期字段之间的日期连接起来。
表格 ABC
ID | 从 | 到 |
---|---|---|
1 | 2021/03/12 | 2021/05/22 |
1 | 2022/06/05 | 2022/06/15 |
2 | 2023/01/01 | 2023/04/18 |
3 | 2020/03/29 | 2020/06/06 |
3 | 2023/05/31 | 2023/07/11 |
3 | 2022/12/12 | 2023/03/20 |
我希望获取从“从”列到“到”列之间的日期,并将其以ID字段分组的形式显示在结果列中;
输出应该类似于以下内容。
ID | 结果 |
---|---|
1 | 2021/03/01; 2021/04/01; 2021/05/01; 2021/06/01 |
2 | 2023/01/01; 2023/02/01; 2023/03/01; 2023/04/01 |
3 | 2020/03/01; 2020/04/01; 2020/05/01; 2020/06/01; 2023/05/01; 2023/06/01; 2023/07/01; 2022/12/01; 2023/01/01; 2023/02/01; 2023/03/01 |
提前感谢…
英文:
Below is an example, I have a similar data and I am looking to concat dates between two date fields
Table ABC
ID | From | To |
---|---|---|
1 | 12/03/2021 | 22/05/2021 |
1 | 05/06/2022 | 15/06/2022 |
2 | 01/01/2023 | 18/04/2023 |
3 | 29/03/2020 | 06/06/2020 |
3 | 31/05/2023 | 11/07/2023 |
3 | 12/12/2022 | 20/03/2023 |
Am looking to get dates between From and To column and print it in Results column with separated by grouping based on ID field ;
The output should look something similar like below.
ID | Result |
---|---|
1 | 01/03/2021; 01/04/2021; 01/05/2021; 01/06/2021 |
2 | 01/01/2023; 01/02/2023; 01/03/2023; 01/04/2023 |
3 | 01/03/2020; 01/04/2020; 01/05/2020; 01/06/2020; 01/05/2023; 01/06/2023; 01/07/2023; 01/12/2022; 01/01/2023; 01/02/2023; 01/03/2023 |
Thanks in advance...
答案1
得分: 0
以下是您要翻译的内容:
这里有一个选项:
示例数据:
SQL> with abc (id, date_from, date_to) as
2 (select 1, date '2021-03-12', date '2021-05-22' from dual union all
3 select 1, date '2022-06-05', date '2022-06-15' from dual union all
4 select 2, date '2023-01-01', date '2023-04-18' from dual union all
5 select 3, date '2020-03-29', date '2020-06-06' from dual union all
6 select 3, date '2023-05-31', date '2023-07-11' from dual union all
7 select 3, date '2022-12-12', date '2023-03-20' from dual
8 )
查询:查找FROM
和TO
日期之间的月份数(截断为每月的第一天),并在ADD_MONTHS
函数中使用它创建所需日期的列表:
9 select id,
10 listagg(add_months(trunc(date_from, 'mm'), column_value - 1), ', ')
11 within group (order by add_months(trunc(date_from, 'mm'), column_value - 1)) result
12 from abc cross join
13 table(cast(multiset(select level from dual
14 connect by level <= months_between(trunc(date_to, 'mm'), trunc(date_from, 'mm')) + 1
15 ) as sys.odcinumberlist))
16 group by id;
ID RESULT
1 01/03/2021, 01/04/2021, 01/05/2021, 01/06/2022
2 01/01/2023, 01/02/2023, 01/03/2023, 01/04/2023
3 01/03/2020, 01/04/2020, 01/05/2020, 01/06/2020, 01/12/2022, 01/01/2023, 01/02/2023, 01/03/2023, 01/05/2023, 01/06/2023, 01/07/2023
SQL>
英文:
Here's one option:
Sample data:
SQL> with abc (id, date_from, date_to) as
2 (select 1, date '2021-03-12', date '2021-05-22' from dual union all
3 select 1, date '2022-06-05', date '2022-06-15' from dual union all
4 select 2, date '2023-01-01', date '2023-04-18' from dual union all
5 select 3, date '2020-03-29', date '2020-06-06' from dual union all
6 select 3, date '2023-05-31', date '2023-07-11' from dual union all
7 select 3, date '2022-12-12', date '2023-03-20' from dual
8 )
Query: find number of months between FROM
and TO
dates (truncated to 1st of month), and use it in ADD_MONTHS
function to create list of required dates:
9 select id,
10 listagg(add_months(trunc(date_from, 'mm'), column_value - 1), ', ')
11 within group (order by add_months(trunc(date_from, 'mm'), column_value - 1)) result
12 from abc cross join
13 table(cast(multiset(select level from dual
14 connect by level <= months_between(trunc(date_to, 'mm'), trunc(date_from, 'mm')) + 1
15 ) as sys.odcinumberlist))
16 group by id;
ID RESULT
--- ----------------------------------------------------------------------------------------------------------------------------------
1 01/03/2021, 01/04/2021, 01/05/2021, 01/06/2022
2 01/01/2023, 01/02/2023, 01/03/2023, 01/04/2023
3 01/03/2020, 01/04/2020, 01/05/2020, 01/06/2020, 01/12/2022, 01/01/2023, 01/02/2023, 01/03/2023, 01/05/2023, 01/06/2023, 01/07/2023
SQL>
答案2
得分: 0
You can use a recursive sub-query factoring clause to generate all the months and then aggregate:
使用递归子查询因子子句来生成所有的月份,然后进行汇总:
WITH months (id, "FROM", "TO") AS (
SELECT id, TRUNC("FROM", 'MM'), TRUNC("TO", 'MM') FROM abc
UNION ALL
SELECT id, ADD_MONTHS("FROM", 1), "TO" FROM months WHERE "FROM" < "TO"
)
SELECT id,
LISTAGG(DISTINCT TO_CHAR("FROM", 'DD/MM/YYYY'), '; ')
WITHIN GROUP (ORDER BY "FROM") AS result
FROM months
GROUP BY id;
Which, for the sample data:
对于样本数据:
CREATE TABLE ABC (ID, "FROM", "TO") AS
SELECT 1, DATE '2021-03-12', DATE '2021-05-22' FROM DUAL UNION ALL
SELECT 1, DATE '2022-06-15', DATE '2022-06-15' FROM DUAL UNION ALL
SELECT 2, DATE '2023-01-01', DATE '2023-04-18' FROM DUAL UNION ALL
SELECT 3, DATE '2020-03-29', DATE '2020-06-06' FROM DUAL UNION ALL
SELECT 3, DATE '2023-05-31', DATE '2023-07-11' FROM DUAL UNION ALL
SELECT 3, DATE '2022-12-12', DATE '2023-03-20' FROM DUAL;
注意:FROM
和 TO
是保留字,不应用作标识符。如果必须使用它们,则在使用它们的每个地方都必须加引号,并且在整个代码中使用一致的大小写。
Outputs:
ID | RESULT |
---|---|
1 | 01/03/2021; 01/04/2021; 01/05/2021; 01/06/2022 |
2 | 01/01/2023; 01/02/2023; 01/03/2023; 01/04/2023 |
3 | 01/03/2020; 01/04/2020; 01/05/2020; 01/06/2020; 01/12/2022; 01/01/2023; 01/02/2023; 01/03/2023; 01/05/2023; 01/06/2023; 01/07/2023 |
英文:
You can use a recursive sub-query factoring clause to generate all the months and then aggregate:
WITH months (id, "FROM", "TO") AS (
SELECT id, TRUNC("FROM", 'MM'), TRUNC("TO", 'MM') FROM abc
UNION ALL
SELECT id, ADD_MONTHS("FROM", 1), "TO" FROM months WHERE "FROM" < "TO"
)
SELECT id,
LISTAGG(DISTINCT TO_CHAR("FROM", 'DD/MM/YYYY'), '; ')
WITHIN GROUP (ORDER BY "FROM") AS result
FROM months
GROUP BY id;
Which, for the sample data:
CREATE TABLE ABC (ID, "FROM", "TO") AS
SELECT 1, DATE '2021-03-12', DATE '2021-05-22' FROM DUAL UNION ALL
SELECT 1, DATE '2022-06-15', DATE '2022-06-15' FROM DUAL UNION ALL
SELECT 2, DATE '2023-01-01', DATE '2023-04-18' FROM DUAL UNION ALL
SELECT 3, DATE '2020-03-29', DATE '2020-06-06' FROM DUAL UNION ALL
SELECT 3, DATE '2023-05-31', DATE '2023-07-11' FROM DUAL UNION ALL
SELECT 3, DATE '2022-12-12', DATE '2023-03-20' FROM DUAL;
Note: FROM
and TO
are reserved words and should not be used as identifiers. If you must use them then they must be quoted everywhere they are used (and a consistent case used throughout).
Outputs:
ID | RESULT |
---|---|
1 | 01/03/2021; 01/04/2021; 01/05/2021; 01/06/2022 |
2 | 01/01/2023; 01/02/2023; 01/03/2023; 01/04/2023 |
3 | 01/03/2020; 01/04/2020; 01/05/2020; 01/06/2020; 01/12/2022; 01/01/2023; 01/02/2023; 01/03/2023; 01/05/2023; 01/06/2023; 01/07/2023 |
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论