/ *格式化于2015/06/24 15:51(Formatter Plus v4.8.5)* /
SELECT (SELECT NAME
FROM dept
WHERE deptid = department) AS NAME, SUM (received),
SUM (notresponded)
FROM (SELECT TRIM (regexp_substr (b.dept, '[^,]+', 1, LEVEL)
) AS department,
COUNT (*) received,
COUNT (*)
- COUNT
(CASE
WHEN ROUND
( ( TO_DATE
(TO_CHAR (clssndtm,
'YYYY-MM-DD hh24:mi'
),
'YYYY-MM-DD hh24:mi'
)
- TO_DATE
(TO_CHAR (made,
'YYYY-MM-DD hh24:mi'
),
'YYYY-MM-DD hh24:mi'
)
)
* 24
) <= 1
THEN ROUND
( ( TO_DATE
(TO_CHAR
(clssndtm,
'YYYY-MM-DD hh24:mi'
),
'YYYY-MM-DD hh24:mi'
)
- TO_DATE
(TO_CHAR
(made,
'YYYY-MM-DD hh24:mi'
),
'YYYY-MM-DD hh24:mi'
)
)
* 24
)
ELSE NULL
END
) notresponded
FROM reportng a, routing b
WHERE a.ticket = b.ticket
AND (1 = 1)
AND a.rptflag = 1
AND a.clssndtm IS NOT NULL
AND companyid = 2682
CONNECT BY INSTR (dept, ',', 1, LEVEL - 1) > 0
AND TRUNC (made) BETWEEN TRUNC (TO_DATE ('06/01/2015',
'mm/dd/yyyy'
)
)
AND TRUNC (TO_DATE ('06/23/2015',
'mm/dd/yyyy'
)
)
GROUP BY TRIM (regexp_substr (dept, '[^,]+', 1, LEVEL)))
GROUP BY department
答案 0 :(得分:0)
首先,您希望查询可读,否则无法查看正在进行的操作:
SELECT
(SELECT NAME FROM dept WHERE deptid = department) AS NAME,
SUM (received),
SUM (notresponded)
FROM
(SELECT
TRIM (regexp_substr (b.dept, '[^,]+', 1, LEVEL) ) AS department,
COUNT () received,
COUNT () - COUNT (CASE WHEN ROUND ( ( TO_DATE (TO_CHAR (clssndtm, 'YYYY-MM-DD hh24:mi' ), 'YYYY-MM-DD hh24:mi' ) - TO_DATE (TO_CHAR (made, 'YYYY-MM-DD hh24:mi' ), 'YYYY-MM-DD hh24:mi' ) ) * 24 ) <= 1 THEN ROUND ( ( TO_DATE (TO_CHAR (clssndtm, 'YYYY-MM-DD hh24:mi' ), 'YYYY-MM-DD hh24:mi' ) - TO_DATE (TO_CHAR (made, 'YYYY-MM-DD hh24:mi' ), 'YYYY-MM-DD hh24:mi' ) ) * 24 ) ELSE NULL END ) notresponded
FROM
reportng a,
routing b
WHERE
a.ticket = b.ticket
AND (1 = 1)
AND a.rptflag = 1
AND a.clssndtm IS NOT NULL
AND companyid = 2682
CONNECT BY INSTR (dept, ',', 1, LEVEL - 1) > 0 AND TRUNC (made) BETWEEN TRUNC (TO_DATE ('06/01/2015', 'mm/dd/yyyy' ) ) AND TRUNC (TO_DATE ('06/23/2015', 'mm/dd/yyyy' ) )
GROUP BY
TRIM (regexp_substr (dept, '[^,]+', 1, LEVEL))
)
GROUP BY
department
现在,那里有一些Oracle特定的东西,我不明白,但我们可以看到这里有两个不同的主要步骤。有第一个内部查询,可能有效也可能无法有效运行。单独运行它以查看它是如何进行的。我会认为它很好(可能不是)。
由于SELECT子句中有相关查询,因此外部位肯定不是最理想的。这需要分成一个连接:
SELECT
dept.NAME,
SUM (received),
SUM (notresponded)
FROM
complex_inner_query
inner join dept on dept.deptid = complex_inner_query.department
GROUP BY
department
这可能很好地解决您的问题,但如果不是,您需要开始分析内部查询以找出效率低下。