如何每天选择oracle 24小时时间间隔?

时间:2019-08-23 05:27:00

标签: sql oracle

“我正在设置查询,该查询每天选择24小时间隔。我该怎么办?

我搜索了它,然后尝试了一下,但是它只选择了一天的数据。

SELECT B.CALL_TIME2,
       A.T8, A.T9,A.T10, A.T11, A.T12, A.T13,
       A.T14, A.T15,A.T16, A.T17, A.T18, A.T19,
       A.T20, A.T21,A.T22, A.T23, A.T24, A.T1,
       A.T2, A.T3,A.T4, A.T5, A.T6, A.T7
     FROM 
          ( 
          SELECT                  
                  SUM(DECODE(TO_CHAR(C.CALL_TIME,'HH24'),8,DECODE(C.CALL_TYPE,'001',1,'002',1,'003',1,0),0)) AS T8,
                   SUM(DECODE(TO_CHAR(C.CALL_TIME,'HH24'),9,DECODE(C.CALL_TYPE,'001',1,'002',1,'003',1,0),0)) AS T9,
                   SUM(DECODE(TO_CHAR(C.CALL_TIME,'HH24'),10,DECODE(C.CALL_TYPE,'001',1,'002',1,'003',1,0),0)) AS T10,
                   SUM(DECODE(TO_CHAR(C.CALL_TIME,'HH24'),11,DECODE(C.CALL_TYPE,'001',1,'002',1,'003',1,0),0)) AS T11,
                   SUM(DECODE(TO_CHAR(C.CALL_TIME,'HH24'),12,DECODE(C.CALL_TYPE,'001',1,'002',1,'003',1,0),0)) AS T12,
                   SUM(DECODE(TO_CHAR(C.CALL_TIME,'HH24'),13,DECODE(C.CALL_TYPE,'001',1,'002',1,'003',1,0),0)) AS T13,
                   SUM(DECODE(TO_CHAR(C.CALL_TIME,'HH24'),14,DECODE(C.CALL_TYPE,'001',1,'002',1,'003',1,0),0)) AS T14,
                   SUM(DECODE(TO_CHAR(C.CALL_TIME,'HH24'),15,DECODE(C.CALL_TYPE,'001',1,'002',1,'003',1,0),0)) AS T15,
                   SUM(DECODE(TO_CHAR(C.CALL_TIME,'HH24'),16,DECODE(C.CALL_TYPE,'001',1,'002',1,'003',1,0),0)) AS T16,
                   SUM(DECODE(TO_CHAR(C.CALL_TIME,'HH24'),17,DECODE(C.CALL_TYPE,'001',1,'002',1,'003',1,0),0)) AS T17,
                   SUM(DECODE(TO_CHAR(C.CALL_TIME,'HH24'),18,DECODE(C.CALL_TYPE,'001',1,'002',1,'003',1,0),0)) AS T18,
                   SUM(DECODE(TO_CHAR(C.CALL_TIME,'HH24'),19,DECODE(C.CALL_TYPE,'001',1,'002',1,'003',1,0),0)) AS T19,
                   SUM(DECODE(TO_CHAR(C.CALL_TIME,'HH24'),20,DECODE(C.CALL_TYPE,'001',1,'002',1,'003',1,0),0)) AS T20,
                   SUM(DECODE(TO_CHAR(C.CALL_TIME,'HH24'),21,DECODE(C.CALL_TYPE,'001',1,'002',1,'003',1,0),0)) AS T21,
                   SUM(DECODE(TO_CHAR(C.CALL_TIME,'HH24'),22,DECODE(C.CALL_TYPE,'001',1,'002',1,'003',1,0),0)) AS T22,
                   SUM(DECODE(TO_CHAR(C.CALL_TIME,'HH24'),23,DECODE(C.CALL_TYPE,'001',1,'002',1,'003',1,0),0)) AS T23,
                   SUM(DECODE(TO_CHAR(C.CALL_TIME,'HH24'),0,DECODE(C.CALL_TYPE,'001',1,'002',1,'003',1,0),0)) AS T24,
                   SUM(DECODE(TO_CHAR(C.CALL_TIME,'HH24'),1,DECODE(C.CALL_TYPE,'001',1,'002',1,'003',1,0),0)) AS T1,
                   SUM(DECODE(TO_CHAR(C.CALL_TIME,'HH24'),2,DECODE(C.CALL_TYPE,'001',1,'002',1,'003',1,0),0)) AS T2,
                   SUM(DECODE(TO_CHAR(C.CALL_TIME,'HH24'),3,DECODE(C.CALL_TYPE,'001',1,'002',1,'003',1,0),0)) AS T3,
                   SUM(DECODE(TO_CHAR(C.CALL_TIME,'HH24'),4,DECODE(C.CALL_TYPE,'001',1,'002',1,'003',1,0),0)) AS T4,
                   SUM(DECODE(TO_CHAR(C.CALL_TIME,'HH24'),5,DECODE(C.CALL_TYPE,'001',1,'002',1,'003',1,0),0)) AS T5,
                   SUM(DECODE(TO_CHAR(C.CALL_TIME,'HH24'),6,DECODE(C.CALL_TYPE,'001',1,'002',1,'003',1,0),0)) AS T6,
                   SUM(DECODE(TO_CHAR(C.CALL_TIME,'HH24'),7,DECODE(C.CALL_TYPE,'001',1,'002',1,'003',1,0),0)) AS T7
               FROM CALL_DATA C, CALL_DATA_TYPE D     
               WHERE CALL_TIME >= TO_DATE(:i_call_time_start,'YYYYMMDDHH24MISS')
                 AND CALL_TIME < TO_DATE(:i_call_time_end,'YYYYMMDDHH24MISS')           
                 AND D.ID = C.CALL_ID
                      AND (:i_checkfalse = 'ALL' OR D.CHECK_FALSE_CALL = :i_checkfalse)
            GROUP BY SUBSTR(RCPT_SEQ,1,1))
            A,
              (    
                 select distinct
   to_char(CALL_TIME,'YYYY-MM-DD') AS CALL_TIME2

from
   CALL_DATA

group by 
   CALL_TIME
order by
   to_char(CALL_TIME,'YYYY-MM-DD') desc              

              ) B

整天的输出数据相同。 output

实际上我每天都需要数据: wanted output

3 个答案:

答案 0 :(得分:1)

查询涉及很多复杂性。我绝对会说您应该使用PIVOT

我已经添加了一些丢失的joins,已使用的PIVOT和我试图删除的不必要的代码。

以下是最适合您的查询:(请注意内联注释以进行描述)

SELECT
    B.CALL_TIME2,
    A.T8,
    A.T9,
    A.T10,
    A.T11,
    A.T12,
    A.T13,
    A.T14,
    A.T15,
    A.T16,
    A.T17,
    A.T18,
    A.T19,
    A.T20,
    A.T21,
    A.T22,
    A.T23,
    A.T24,
    A.T1,
    A.T2,
    A.T3,
    A.T4,
    A.T5,
    A.T6,
    A.T7
FROM
    (
        SELECT
            *
        FROM
            (
                SELECT
                    CALL_TIME, -- IN PIVOT GROUPING BASED ON THIS
                    SUBSTR(RCPT_SEQ, 1, 1) AS RCPT_SEQ, -- IN PIVOT GROUPING BASED ON THIS
                    CALL_TIME_HOUR, -- USED IN PIVOT
                    CALL_TYPE -- USED IN PIVOT
                FROM
                    (
                        SELECT
                            RCPT_SEQ,
                            TO_CHAR(C.CALL_TIME, 'HH24') AS CALL_TIME_HOUR,
                            CASE
                                WHEN C.CALL_TYPE IN (
                                    '001',
                                    '002',
                                    '003'
                                ) THEN 1
                                ELSE 0
                            END AS CALL_TYPE, -- CONVERTED DECODE TO CASE .. WHEN
                            CALL_TIME,
                            CALL_ID
                        FROM
                            CALL_DATA C
                    ) C
                    JOIN CALL_DATA_TYPE D ON ( D.ID = C.CALL_ID ) -- STANDARD ANSI JOIN
                WHERE
                    CALL_TIME >= TO_DATE(:I_CALL_TIME_START, 'YYYYMMDDHH24MISS')
                    AND CALL_TIME < TO_DATE(:I_CALL_TIME_END, 'YYYYMMDDHH24MISS')
                    AND ( :I_CHECKFALSE = 'ALL'
                          OR D.CHECK_FALSE_CALL = :I_CHECKFALSE )
            -- PIVOTING
            ) PIVOT (
                SUM ( CALL_TYPE )
                FOR CALL_TIME_HOUR
                IN ( '1' AS "T1", '2' AS "T2", ......  ,'24' AS "T0" ) -- ADD ALL HOURS (3-23) HERE
            )
    ) A
    JOIN (
        SELECT DISTINCT
            TO_CHAR(CALL_TIME, 'YYYY-MM-DD') AS CALL_TIME2
        FROM
            CALL_DATA -- REMOVED GROUP BY AND ORDER BY
    ) B ON ( A.CALL_TIME = B.CALL_TIME2 ) -- ADDED THIS JOIN
ORDER BY
    B.CALL_TIME2 DESC;  -- ORDERING FINAL RESULT

干杯!

答案 1 :(得分:1)

仅使用条件聚合。我想这就是你想要的:

select trunc(cd.call_time),
       sum(case when extract(hour from cd.call_time) = 8 and
                     cd.call_type in ('001', '002', '003') then 1 else 0
                end) as t08,
       sum(case when extract(hour from cd.call_time) = 9 and cd.call_type in ('001', '002', '003')
                then 1 else 0
           end) as t09,
         . . .
from CALL_DATA cd join
     CALL_DATA_TYPE cdt  
     on cdt.id = cd.call_id  
where cd.CALL_TIME >= to_date(:i_call_time_start, 'YYYYMMDDHH24MISS') and
      cd.CALL_TIME < TO_DATE(:i_call_time_end, 'YYYYMMDDHH24MISS') and
     (:i_checkfalse = 'ALL' or D.CHECK_FALSE_CALL = :i_checkfalse)
group by trunc(cd.call_time);

注意:

  • 学习使用正确的,明确的,标准 JOIN语法。
  • 通话时间具有时间成分,因此您需要提取日期。
  • 条件逻辑的标准SQL语法为CASEDECODE()是定制的Oracle逻辑。
  • DECODE()用于IN很难。
  • 我认为CALL_DATA_TYPE表不是必需的,但是我已经把它留在里面了。

答案 2 :(得分:0)

我希望你需要这个:-)

SELECT B.CALL_TIME2,
       A.T8, A.T9,A.T10, A.T11, A.T12, A.T13,
       A.T14, A.T15,A.T16, A.T17, A.T18, A.T19,
       A.T20, A.T21,A.T22, A.T23, A.T24, A.T1,
       A.T2, A.T3,A.T4, A.T5, A.T6, A.T7
     FROM 
          ( 
          SELECT  to_char(CALL_TIME,'YYYY-MM-DD') CALL_TIME,               
                  SUM(DECODE(TO_CHAR(C.CALL_TIME,'HH24'),8,DECODE(C.CALL_TYPE,'001',1,'002',1,'003',1,0),0)) AS T8,
                   SUM(DECODE(TO_CHAR(C.CALL_TIME,'HH24'),9,DECODE(C.CALL_TYPE,'001',1,'002',1,'003',1,0),0)) AS T9,
                   SUM(DECODE(TO_CHAR(C.CALL_TIME,'HH24'),10,DECODE(C.CALL_TYPE,'001',1,'002',1,'003',1,0),0)) AS T10,
                   SUM(DECODE(TO_CHAR(C.CALL_TIME,'HH24'),11,DECODE(C.CALL_TYPE,'001',1,'002',1,'003',1,0),0)) AS T11,
                   SUM(DECODE(TO_CHAR(C.CALL_TIME,'HH24'),12,DECODE(C.CALL_TYPE,'001',1,'002',1,'003',1,0),0)) AS T12,
                   SUM(DECODE(TO_CHAR(C.CALL_TIME,'HH24'),13,DECODE(C.CALL_TYPE,'001',1,'002',1,'003',1,0),0)) AS T13,
                   SUM(DECODE(TO_CHAR(C.CALL_TIME,'HH24'),14,DECODE(C.CALL_TYPE,'001',1,'002',1,'003',1,0),0)) AS T14,
                   SUM(DECODE(TO_CHAR(C.CALL_TIME,'HH24'),15,DECODE(C.CALL_TYPE,'001',1,'002',1,'003',1,0),0)) AS T15,
                   SUM(DECODE(TO_CHAR(C.CALL_TIME,'HH24'),16,DECODE(C.CALL_TYPE,'001',1,'002',1,'003',1,0),0)) AS T16,
                   SUM(DECODE(TO_CHAR(C.CALL_TIME,'HH24'),17,DECODE(C.CALL_TYPE,'001',1,'002',1,'003',1,0),0)) AS T17,
                   SUM(DECODE(TO_CHAR(C.CALL_TIME,'HH24'),18,DECODE(C.CALL_TYPE,'001',1,'002',1,'003',1,0),0)) AS T18,
                   SUM(DECODE(TO_CHAR(C.CALL_TIME,'HH24'),19,DECODE(C.CALL_TYPE,'001',1,'002',1,'003',1,0),0)) AS T19,
                   SUM(DECODE(TO_CHAR(C.CALL_TIME,'HH24'),20,DECODE(C.CALL_TYPE,'001',1,'002',1,'003',1,0),0)) AS T20,
                   SUM(DECODE(TO_CHAR(C.CALL_TIME,'HH24'),21,DECODE(C.CALL_TYPE,'001',1,'002',1,'003',1,0),0)) AS T21,
                   SUM(DECODE(TO_CHAR(C.CALL_TIME,'HH24'),22,DECODE(C.CALL_TYPE,'001',1,'002',1,'003',1,0),0)) AS T22,
                   SUM(DECODE(TO_CHAR(C.CALL_TIME,'HH24'),23,DECODE(C.CALL_TYPE,'001',1,'002',1,'003',1,0),0)) AS T23,
                   SUM(DECODE(TO_CHAR(C.CALL_TIME,'HH24'),0,DECODE(C.CALL_TYPE,'001',1,'002',1,'003',1,0),0)) AS T24,
                   SUM(DECODE(TO_CHAR(C.CALL_TIME,'HH24'),1,DECODE(C.CALL_TYPE,'001',1,'002',1,'003',1,0),0)) AS T1,
                   SUM(DECODE(TO_CHAR(C.CALL_TIME,'HH24'),2,DECODE(C.CALL_TYPE,'001',1,'002',1,'003',1,0),0)) AS T2,
                   SUM(DECODE(TO_CHAR(C.CALL_TIME,'HH24'),3,DECODE(C.CALL_TYPE,'001',1,'002',1,'003',1,0),0)) AS T3,
                   SUM(DECODE(TO_CHAR(C.CALL_TIME,'HH24'),4,DECODE(C.CALL_TYPE,'001',1,'002',1,'003',1,0),0)) AS T4,
                   SUM(DECODE(TO_CHAR(C.CALL_TIME,'HH24'),5,DECODE(C.CALL_TYPE,'001',1,'002',1,'003',1,0),0)) AS T5,
                   SUM(DECODE(TO_CHAR(C.CALL_TIME,'HH24'),6,DECODE(C.CALL_TYPE,'001',1,'002',1,'003',1,0),0)) AS T6,
                   SUM(DECODE(TO_CHAR(C.CALL_TIME,'HH24'),7,DECODE(C.CALL_TYPE,'001',1,'002',1,'003',1,0),0)) AS T7
               FROM CALL_DATA C, CALL_DATA_TYPE D     
               WHERE CALL_TIME >= TO_DATE(:i_call_time_start,'YYYYMMDDHH24MISS')
                 AND CALL_TIME < TO_DATE(:i_call_time_end,'YYYYMMDDHH24MISS')           
                 AND D.ID = C.CALL_ID
                      AND (:i_checkfalse = 'ALL' OR D.CHECK_FALSE_CALL = :i_checkfalse)
            GROUP BY SUBSTR(RCPT_SEQ,1,1),to_char(CALL_TIME,'YYYY-MM-DD'))
            A,
              (    
                 select distinct
   to_char(CALL_TIME,'YYYY-MM-DD') AS CALL_TIME2

from
   CALL_DATA
     ) B where b.CALL_TIME2=a.CALL_TIME
order by b.LL_TIME2 desc