SQL联合查询不返回两列

时间:2015-11-04 23:49:52

标签: sql oracle11g

我正在尝试以特定格式从数据库中检索数据以挂钩到仪表板应用程序。我需要一个名为“Import”的列,其下方有计数,另一个名为“Export”,其中包含相关计数。我现在有以下查询,但它只返回一个导入列,我已经确认有相关数据显示为导出。有任何想法吗?添加我得到的结果的屏幕截图...

enter image description here

SELECT count(*) as "Import", a.ship_id "Ship"
  FROM  SERVICE_EVENTS a JOIN CONTAINERS b ON a.eq_nbr = b.nbr
 WHERE  (
        (to_char(sysdate, 'HH24') between '07' and '17' and a.performed between trunc(sysdate) + 7/24 and sysdate and category = 'I')
        OR
        (to_char(sysdate, 'HH24') between '18' and '23' and a.performed between trunc(sysdate) + 18/24 and sysdate and category = 'I')
        OR
        (to_char(sysdate, 'HH24') between '00' and '06' and a.performed between trunc(sysdate - 1) + 18/24 and sysdate and category = 'I')
        )
        AND a.TSERV_ID in ('LOAD', 'DISCHARGE') group by a.ship_id 
UNION
SELECT count(*) as "Export", a.ship_id "Ship"
  FROM  SERVICE_EVENTS a JOIN CONTAINERS b ON a.eq_nbr = b.nbr
 WHERE  (
        (to_char(sysdate, 'HH24') between '07' and '17' and a.performed between trunc(sysdate) + 7/24 and sysdate and category = 'E')
        OR
        (to_char(sysdate, 'HH24') between '18' and '23' and a.performed between trunc(sysdate) + 18/24 and sysdate and category = 'E')
        OR
        (to_char(sysdate, 'HH24') between '00' and '06' and a.performed between trunc(sysdate - 1) + 18/24 and sysdate and category = 'E')
        )
        AND a.TSERV_ID in ('LOAD', 'DISCHARGE') group by a.ship_id 

;

2 个答案:

答案 0 :(得分:3)

您可以执行条件聚合并简化查询:

SELECT
    a.ship_id AS Ship,
    COUNT(CASE WHEN category = 'I' THEN 1 END) AS Import,
    COUNT(CASE WHEN category = 'E' THEN 1 END) AS Export
FROM SERVICE_EVENTS a 
JOIN CONTAINERS b 
    ON a.eq_nbr = b.nbr
WHERE
    (
        (to_char(sysdate, 'HH24') BETWEEN '07' AND '17' AND a.performed BETWEEN trunc(sysdate) + 7/24 AND sysdate)      
        OR (to_char(sysdate, 'HH24') BETWEEN '18' AND '23' AND a.performed BETWEEN trunc(sysdate) + 18/24 AND sysdate)
        OR (to_char(sysdate, 'HH24') BETWEEN '00' AND '06' AND a.performed BETWEEN trunc(sysdate - 1) + 18/24 AND sysdate)
    )
    AND a.TSERV_ID in ('LOAD', 'DISCHARGE')
GROUP BY a.ship_id 

答案 1 :(得分:2)

您可以明确选择null作为未计数的列,再进行一次聚合。

select max(Import) as Import, max(Export) as Export, Ship
from (  
SELECT count(*) as "Import", null as "Export", a.ship_id "Ship"
  FROM  SERVICE_EVENTS a JOIN CONTAINERS b ON a.eq_nbr = b.nbr
 WHERE  (
        (to_char(sysdate, 'HH24') between '07' and '17' and a.performed between trunc(sysdate) + 7/24 and sysdate and category = 'I')
        OR
        (to_char(sysdate, 'HH24') between '18' and '23' and a.performed between trunc(sysdate) + 18/24 and sysdate and category = 'I')
        OR
        (to_char(sysdate, 'HH24') between '00' and '06' and a.performed between trunc(sysdate - 1) + 18/24 and sysdate and category = 'I')
        )
        AND a.TSERV_ID in ('LOAD', 'DISCHARGE') group by a.ship_id 
UNION
SELECT null as import, count(*) as "Export", a.ship_id "Ship"
  FROM  SERVICE_EVENTS a JOIN CONTAINERS b ON a.eq_nbr = b.nbr
 WHERE  (
        (to_char(sysdate, 'HH24') between '07' and '17' and a.performed between trunc(sysdate) + 7/24 and sysdate and category = 'E')
        OR
        (to_char(sysdate, 'HH24') between '18' and '23' and a.performed between trunc(sysdate) + 18/24 and sysdate and category = 'E')
        OR
        (to_char(sysdate, 'HH24') between '00' and '06' and a.performed between trunc(sysdate - 1) + 18/24 and sysdate and category = 'E')
        )
        AND a.TSERV_ID in ('LOAD', 'DISCHARGE') group by a.ship_id 
  ) t
group by Ship