试图合并2个视图

时间:2016-10-19 15:51:53

标签: sql oracle oracle-apex

我需要检索此视图提供的数据: BLICK_1_DESCR_LIST。

我没有找到如何直接创建它。所以我创建了视图BLICK_1_DESCR_NO_LIST,它在第二个视图BLICK_1_DESCR_LIST中使用。

我想在一个更好的视图中做到这一点。

CREATE VIEW BLICK_1_DESCR_NO_LIST
AS SELECT ITEM_ID , MIN(ITEM_DESCR_NO) MIN_I_D_NO, 
COUNT(ITEM_DESCR_NO) COUNT_I_D_NO FROM BLICK_ITEM_DESCR
 GROUP BY ITEM_ID
UNION
 SELECT ID , 0 ZERO, 0 ZERO2 FROM  BLICK_ITEM
 LEFT JOIN BLICK_ITEM_DESCR ON BLICK_ITEM.ID = BLICK_ITEM_DESCR.ITEM_ID
 WHERE ITEM_DESCR_NO IS NULL;


CREATE VIEW BLICK_1_DESCR_LIST
AS SELECT V1.ITEM_ID, V1.MIN_I_D_NO, V1.COUNT_I_D_NO, T1.ITEM_DESCR
FROM BLICK_1_DESCR_NO_LIST V1
LEFT JOIN BLICK_ITEM_DESCR T1 ON V1.ITEM_ID = T1.ITEM_ID 
   AND V1.MIN_I_D_NO = T1.ITEM_DESCR_NO
    ORDER BY ITEM_ID;

1 个答案:

答案 0 :(得分:0)

您可以将第一个视图合并为子查询:

CREATE VIEW BLICK_1_DESCR_LIST AS
    SELECT V1.ITEM_ID, V1.MIN_I_D_NO, V1.COUNT_I_D_NO, T1.ITEM_DESCR
    FROM ((SELECT ITEM_ID, MIN(ITEM_DESCR_NO) as MIN_I_D_NO, 
COUNT(ITEM_DESCR_NO) as COUNT_I_D_NO
           FROM BLICK_ITEM_DESCR
           GROUP BY ITEM_ID
          ) UNION
          (SELECT ID, 0, 0
           FROM BLICK_ITEM LEFT JOIN
                BLICK_ITEM_DESCR
                ON BLICK_ITEM.ID = BLICK_ITEM_DESCR.ITEM_ID
           WHERE ITEM_DESCR_NO IS NULL
          )) V1 LEFT JOIN
         BLICK_ITEM_DESCR T1 
         ON V1.ITEM_ID = T1.ITEM_ID AND
            V1.MIN_I_D_NO = T1.ITEM_DESCR_NO
    ORDER BY ITEM_ID;