将查询(包含子查询)与另一个查询组合在一起

时间:2015-03-11 20:06:05

标签: sql oracle

以下两个查询的工作都是相互独立的。我需要将它们合并为一个,并完成以下任务。

  1. 计算员工日期范围的发送次数(DATA_CANNON_DISPATCHED)。

  2. 计算日期范围内员工已完成的调度数。我已经设置好在php中处理这个问题了。在查询中执行此操作将是一件好事,但不是必需的。

  3. 计算日期范围内员工的清算行为代码数量10,35,80。

  4. 代码:

    /*  1  */
    SELECT  COUNT(INCIDENT_NUMBER) AS NTFR
    FROM ETA
    WHERE CLEARING_ACTION_CODE IN ('10-Trouble Cleared','35-False Report','80-No Trouble Found - C.O.')
    AND INCIDENT_NUMBER IN (SELECT INCIDENT_NUMBER
                            FROM  HDESK
                            WHERE DATA_CANNON_DISPATCHED = 'Yes' 
                            AND CUST_EMPLOYEE_ID = 'n9950298' 
                            AND REPORTED_DATE_TIME_RAW_VALUE
                            BETWEEN 1420510400 AND 1420906799)
    
    
    /* 2 */
    SELECT INCIDENT_NUMBER, STATUS,DATA_CANNON_DISPATCHED 
    FROM HDESK
    WHERE DATA_CANNON_DISPATCHED = 'Yes' 
    AND CUST_EMPLOYEE_ID = 'n9950298' 
    AND REPORTED_DATE_TIME_RAW_VALUE
    BETWEEN 1420510400 AND 1420906799
    
    
    /*This is what I came up with that works. */
    /* 3 */
    SELECT H.INCIDENT_NUMBER, H.STATUS,DATA_CANNON_DISPATCHED , CLEARING_ACTION_CODE
    FROM ARADMIN.WIN_LT_RPTG_HELP_DESK H, ARADMIN.WIN_LT_RPTG_ETA_APPT_INFO E
    WHERE H.DATA_CANNON_DISPATCHED = 'Yes' 
    AND H.CUST_EMPLOYEE_ID = 'n9950298' 
    AND H.REPORTED_DATE_TIME_RAW_VALUE BETWEEN 1420510400 AND 1420906799
    AND H.INCIDENT_NUMBER = E.INCIDENT_NUMBER (+)
    

2 个答案:

答案 0 :(得分:0)

试试这个

SELECT COUNT(e.INCIDENT_NUMBER)as NTFR,
       h.INCIDENT_NUMBER, 
       h.STATUS,
       h.DATA_CANNON_DISPATCHED,
       COUNT(e.CLEARING_ACTION_CODE) as CLEARING_ACTION_CODE
FROM ETA e 
INNER JOIN HDESK h 
on e.INCIDENT_NUMBER= h.INCIDENT_NUMBER
WHERE h.DATA_CANNON_DISPATCHED = 'Yes' 
                        AND  e.CLEARING_ACTION_CODE 
  IN ('10-Trouble Cleared','35-False Report','80-No Trouble Found - C.O.')

                        AND h.CUST_EMPLOYEE_ID = 'n9950298' 
                        AND h.REPORTED_DATE_TIME_RAW_VALUE
                        BETWEEN 1420510400 AND 1420906799

GROUP BY e.INCIDENT_NUMBER,e.CLEARING_ACTION_CODE,h.STATUS,h.DATA_CANNON_DISPATCHED

答案 1 :(得分:0)

对于给定的emplyoee和句点,您可以使用此查询。我不确定你想要什么, 如果出现问题,请尝试删除distinct

如果您需要以下表格:Id - Dispatches - Completed - Codes 然后通过在id部分中添加select并添加group by子句来轻松转换查询。

select 
    count(distinct incident_number) dispatches,
    count(decode(data_cannon_dispatched, 'Yes', 1)) completed,
    count(distinct clearing_action_code) codes
  from eta e join hdesk h using (incident_number)
  where substr(e.clearing_action_code, 1, 3) in ('10-', '35-', '80-')
    and cust_employee_id = 'n9950298' 
    and reported_date_time_raw_value between 1420510400 and 1420906799

测试数据和结果:

with eta as (select 1 incident_number, '10-Trouble Cleared' clearing_action_code from dual
  union select 2, '10-Trouble Cleared' from dual
  union select 3, '80-No Trouble Found - C.O.' from dual),
hdesk as (
  select 1 incident_number, 'Yes' data_cannon_dispatched, 
    'n9950298' cust_employee_id, 1420510400 reported_date_time_raw_value, 
    5 status from dual
  union select 2, 'Yes', 'n9950298', 1420510401, 6 from dual
  union select 3, 'No', 'n9950298', 1420510402, 6 from dual
  union select 3, 'No', 'aqq50298', 1420510403, 0 from dual)
select 
    count(distinct incident_number) dispatches,
    count(decode(data_cannon_dispatched, 'Yes', 1)) completed,
    count(distinct clearing_action_code) codes
  from eta e join hdesk h using (incident_number)
  where substr(e.clearing_action_code, 1, 3) in ('10-', '35-', '80-')
    and cust_employee_id = 'n9950298' 
    and reported_date_time_raw_value between 1420510400 and 1420906799

结果:

DISPATCHES COMPLETED  CODES
---------- ---------- ---------- 
         3          2          2

注意:

  • 最好将清除操作代码和清除操作说明存储在不同的表中,这样您就不必总是将这些字符串存储在表中。
  • 存储日期的方式有点难以理解; - )
  • 当你添加一些输入和输出数据时,对于回答的人来说总是更简单。

SQL Fiddle