如何在Oracle SQL中使用connect by函数创建每季度一次的输出?

gopyfrb3  于 2023-06-05  发布在  Oracle
关注(0)|答案(3)|浏览(685)

我在Oracle SQL中的connect by函数有问题。我有以下数据:
| ID|开始日期|结束日期|价值|
| - -----|- -----|- -----|- -----|
| 1| 2019 - 07 - 22 10:30:00| 2019 - 07 - 22 11:45:00|不适用|
START- & END_DATE总是每季度一次。我需要一个输出如下的SQL查询。
| 季度_小时 *| 价值|
| - -----|- -----|
| 2019 - 07 - 22 10:45:00|不适用|
| 2019 - 07 - 22 11:00:00|不适用|
| 2019 - 07 - 22 11:15:00|不适用|
| 2019 - 07 - 22 11:30:00|不适用|
| 2019 - 07 - 22 11:45:00|不适用|

  • 一刻钟的结束时间

我试过这个,但不幸的是,这不会工作:

WITH interval_data AS (
  SELECT
    START_DATE + ((LEVEL - 1) / 96) AS quarter_hour,
    VALUE
  FROM
    table
  CONNECT BY
    LEVEL <= ((END_DATE - START_DATE) *96) + 1
)
SELECT
  TO_CHAR(quarter_hour, 'DD.MM.YYYY HH24:MI') AS quarter_hour,
  VALUE
FROM
  interval_data
ORDER BY
  quarter_hour;

有人知道我哪里做错了吗?

sycxhyv7

sycxhyv71#

您可以使用递归子查询来实现:

create table t (id, start_date, end_date, value) AS 
(SELECT 1, TO_DATE('14.07.2022 10:30:00','DD.MM.YYYY HH24:MI:SS'),TO_DATE('14.07.2022 11:45:00','DD.MM.YYYY HH24:MI:SS'),'N' FROM DUAL UNION ALL
 SELECT 2, TO_DATE('15.07.2022 10:30:00','DD.MM.YYYY HH24:MI:SS'),TO_DATE('15.07.2022 11:00:00','DD.MM.YYYY HH24:MI:SS'),'Y' FROM DUAL
);


WITH t_qtr (id, qtr_start_date, end_date, VALUE) AS
(
  select id, 
         start_date,
         end_date,
         value
    from t
  UNION ALL
  select id, 
         qtr_start_date + interval '15' minute,
         end_date,
         value
    from t_qtr
   where qtr_start_date < end_date
  
)
SEARCH DEPTH FIRST BY qtr_start_date SET t_order
SELECT id,
       TO_CHAR(qtr_start_date,'DD.MM.YYYY HH24:MI:SS') as QUARTER_HOUR,
       value
FROM   t_qtr;

        ID QUARTER_HOUR        V
---------- ------------------- -
         1 14.07.2022 10:30:00 N
         1 14.07.2022 10:45:00 N
         1 14.07.2022 11:00:00 N
         1 14.07.2022 11:15:00 N
         1 14.07.2022 11:30:00 N
         1 14.07.2022 11:45:00 N
         2 15.07.2022 10:30:00 Y
         2 15.07.2022 10:45:00 Y
         2 15.07.2022 11:00:00 Y
dgiusagp

dgiusagp2#

所以,我运行了下面的代码,看起来不错:

WITH tb as (
  select date '2023-01-31' start_date
   ,date '2023-02-04' end_date
 from dual    
) 
,interval_data AS (
  SELECT
  START_DATE + ((LEVEL - 1) / 96) AS quarter_hour,
  start_date,
  end_date
FROM tb
CONNECT BY LEVEL <= ((END_DATE - START_DATE) *96) + 1

SELECT
     TO_CHAR(quarter_hour, 'DD.MM.YYYY HH24:MI') AS quarter_hour,
         start_date,
         end_date
   FROM interval_data
   ORDER BY quarter_hour

;

tmb3ates

tmb3ates3#

非常感谢您的回复。我真的很困惑。当我把my_table放在另一个WITH子查询子句中时,它可以工作。
在这里我立即得到一个结果。

WITH tb as (
  select start_date, end_date, value 
 from my_table where ID = 1    
) 
,interval_data AS (
  SELECT
  START_DATE + ((LEVEL) / 96) AS quarter_hour,
  VALUE
FROM tb
CONNECT BY LEVEL <= ((END_DATE - START_DATE) *96)
)

   SELECT
     TO_CHAR(quarter_hour, 'DD.MM.YYYY HH24:MI') AS quarter_hour,
         VALUE
   FROM interval_data
   ORDER BY quarter_hour
;

但是当我只使用一个或没有WITH子句时,那么在30分钟内没有任何事情发生:

WITH interval_data AS (
  SELECT
    START_DATE + ((LEVEL) / 96) AS quarter_hour,
    VALUE
  FROM
    my_table where ID = 1
  CONNECT BY
    LEVEL <= ((END_DATE - START_DATE) *96) 
)
SELECT
  TO_CHAR(quarter_hour, 'DD.MM.YYYY HH24:MI') AS quarter_hour,
  VALUE
FROM
  interval_data
ORDER BY
  quarter_hour;

相关问题