In oracle SQL am trying to print dates in one column which is between two date column and grouping based on ID

huangapple go评论53阅读模式
英文:

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 )

查询:查找FROMTO日期之间的月份数(截断为每月的第一天),并在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&gt; with abc (id, date_from, date_to) as
  2    (select 1, date &#39;2021-03-12&#39;, date &#39;2021-05-22&#39; from dual union all
  3     select 1, date &#39;2022-06-05&#39;, date &#39;2022-06-15&#39; from dual union all
  4     select 2, date &#39;2023-01-01&#39;, date &#39;2023-04-18&#39; from dual union all
  5     select 3, date &#39;2020-03-29&#39;, date &#39;2020-06-06&#39; from dual union all
  6     select 3, date &#39;2023-05-31&#39;, date &#39;2023-07-11&#39; from dual union all
  7     select 3, date &#39;2022-12-12&#39;, date &#39;2023-03-20&#39; 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, &#39;mm&#39;), column_value - 1), &#39;, &#39;)
 11      within group (order by add_months(trunc(date_from, &#39;mm&#39;), column_value - 1)) result
 12  from abc cross join
 13  table(cast(multiset(select level from dual
 14                      connect by level &lt;= months_between(trunc(date_to, &#39;mm&#39;), trunc(date_from, &#39;mm&#39;)) + 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&gt;

答案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;

注意:FROMTO 是保留字,不应用作标识符。如果必须使用它们,则在使用它们的每个地方都必须加引号,并且在整个代码中使用一致的大小写。

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

fiddle

英文:

You can use a recursive sub-query factoring clause to generate all the months and then aggregate:

WITH months (id, &quot;FROM&quot;, &quot;TO&quot;) AS (
  SELECT id, TRUNC(&quot;FROM&quot;, &#39;MM&#39;), TRUNC(&quot;TO&quot;, &#39;MM&#39;) FROM abc
UNION ALL
  SELECT id, ADD_MONTHS(&quot;FROM&quot;, 1), &quot;TO&quot; FROM months WHERE &quot;FROM&quot; &lt; &quot;TO&quot;
)
SELECT id,
       LISTAGG(DISTINCT TO_CHAR(&quot;FROM&quot;, &#39;DD/MM/YYYY&#39;), &#39;; &#39;)
         WITHIN GROUP (ORDER BY &quot;FROM&quot;) AS result
FROM   months
GROUP BY id;

Which, for the sample data:

CREATE TABLE ABC (ID, &quot;FROM&quot;, &quot;TO&quot;) AS
SELECT 1, DATE &#39;2021-03-12&#39;, DATE &#39;2021-05-22&#39; FROM DUAL UNION ALL
SELECT 1, DATE &#39;2022-06-15&#39;, DATE &#39;2022-06-15&#39; FROM DUAL UNION ALL
SELECT 2, DATE &#39;2023-01-01&#39;, DATE &#39;2023-04-18&#39; FROM DUAL UNION ALL
SELECT 3, DATE &#39;2020-03-29&#39;, DATE &#39;2020-06-06&#39; FROM DUAL UNION ALL
SELECT 3, DATE &#39;2023-05-31&#39;, DATE &#39;2023-07-11&#39; FROM DUAL UNION ALL
SELECT 3, DATE &#39;2022-12-12&#39;, DATE &#39;2023-03-20&#39; 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

fiddle

huangapple
  • 本文由 发表于 2023年4月17日 19:16:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76034563.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定