Oracle SubQuery(显示子查询列)

时间:2012-03-16 20:57:28

标签: sql oracle subquery

我似乎无法通过搜索找到如何从以下子查询中返回一些列。特别是B.TAP_STAT_HSL / C.TAP_STAT_HSL。我不确定我是否应该加入,但任何帮助将不胜感激。

   SELECT 

   A.HSE_KEY_HSE AS HOUSEKEY,
   A.DROP_STAT_HSE AS DROPSTATUS
   A.TAP_STAT_HSL AS ITAPSTAT

   FROM OPS$SEA.HSE_BASE,OPS$SEA.HSL_LOB,OPS$SEA.OOR_ORDER_OPEN A

   WHERE A.HSE_KEY_HSE = A.HSE_KEY_HSL 
   AND A.HSE_KEY_HSL = A.HSE_KEY_OOR
   AND A.DROP_STAT_HSE = '1'
   AND A.LOB_IND_HSL = 'I'
   AND A.TAP_STAT_HSL IN ('0','2')
   AND A.ORD_STAT_OOR <> 'O'
   AND EXISTS (SELECT 1

   FROM OPS$SEA.HSE_BASE B,OPS$SEA.HSL_LOB B, OPS$SEA.OOR_ORDER_OPEN B

   WHERE A.HSE_KEY_HSE = B.HSE_KEY_HSE
   AND B.HSE_KEY_HSE = B.HSE_KEY_HSL
   AND B.HSE_KEY_HSL = B.HSE_KEY_OOR 
   AND B.DROP_STAT_HSE = '1'
   AND B.LOB_IND_HSL = 'C'
   AND B.TAP_STAT_HSL IN ('0','2')
   AND B.ORD_STAT_OOR <> 'O')
   AND EXISTS (

   SELECT 1

   FROM OPS$SEA.HSE_BASE C,OPS$SEA.HSL_LOB C, OPS$SEA.OOR_ORDER_OPEN C

   WHERE A.HSE_KEY_HSE = C.HSE_KEY_HSE
   AND C.HSE_KEY_HSE = C.HSE_KEY_HSL 
   AND C.HSE_KEY_HSL = C.HSE_KEY_OOR
   AND C.DROP_STAT_HSE = '1'
   AND C.LOB_IND_HSL = 'T'
   AND C.TAP_STAT_HSL IN ('0','2')
   AND C.ORD_STAT_OOR <> 'O')}

1 个答案:

答案 0 :(得分:0)

嗯....

相信您的查询可以重写如下:

WITH Allowed_Rows (houseKey, dropStatus, ipApStat, indicator) 
                   as (SELECT a.HSE_KEY_HSE, a.DROP_STAT_HSE, 
                              b.TAP_STAT_HSL, b.LOB_IND_HSL
                       FROM OPS$SEA.HSE_BASE as a
                       JOIN OPS$SEA.HSL_LOB as b
                       ON b.HSE_KEY_HSL = a.HSE_KEY_HSE
                       AND b.LOB_IND_HSL IN ('I', 'C', 'T')
                       AND b.TAB_STAT_HSL IN ('0', '2')
                       JOIN OPS$SEA.OOR_Order_Open as c
                       ON c.HSE_KEY_OOR = a.HSE_KEY_HSE
                       AND c.ORD_STAT_OOR <> '0'
                       WHERE a.DROP_STAT_HSE = '1')
SELECT houseKey, dropStatus, ipApStat
FROM Allowed_Rows as a
WHERE a.indicator = 'I'
AND EXISTS (SELECT '1' 
            FROM Allowed_Rows as b
            WHERE b.houseKey = a.houseKey
            AND b.indicator = 'C')
AND EXISTS (SELECT '1'
            FROM Allowed_Rows as b
            WHERE b.houseKey = a.houseKey
            AND b.indicator = 'T')

你没有正确地限定你的某些表,并对多个表使用了相同的别名(我很惊讶没有产生语法错误),所以我不得不最好地猜测实际上的位置属于。根据其他(未列出的)要求和约束,还有其他几种可能的变体。

为什么以及如何返回TAP_STAT_HSL的“其他”值?你需要所有可能的组合吗? BC 的行代替A行?什么?