如何在CASE中进行JOIN?或者使用UNION? (SQL)

时间:2013-06-17 13:16:02

标签: sql

我目前对我刚开始支持的应用程序中的模块有两个不同的查询(第一个编程工作)。第一个查询选择引擎所在的所有汽车(9,10,11,12,13)。第二个获得发动机所在的所有汽车(4,44)。它们都工作正常,唯一的问题是当它们放在JSP的同一个表中时,它们不使用相同的过滤器。所以基本上,第一个查询的所有数据将始终排在第一位,第二个查询的数据将始终排在第二位。如果按字母顺序排序,它将对第一个查询A-Z中的所有数据进行排序,然后对第二个查询中的数据A-Z进行排序,因此您会看到如下内容:

A Car with Engine 11
B Car with Engine 9
C Car with Engine 13
D Car with Engine 12
A Car with Engine 44
B Car with Engine 44
C Car with Engine 4

所以我认为最好只做一些CASE和JOIN组合,只需要一个查询。不幸的是,我无法找到最好的方法。数据正从Oracle数据库中的视图中提取。以下是两个查询:

SELECT DISTINCT
P.CAR_ID CAR_ID,
P.TYPE TYPE,
P.LAST_TIME_SERVICED LAST_TIME_SERVICED,
P.SERVICE_TYPE SERVICE_TYPE,
P.PURCHASE_DATE PURCHASE_DATE,
'20'|| SUBSTR (P.MODEL_YEAR, 1, 2)|| ' - '|| '20'|| SUBSTR (P.MODEL_YEAR, 3, 4)          MODEL_YEAR,
P.DESCRIPTION DESCRIPTION,
P.ENGINE_TYPE ENGINE_TYPE,
V.MODEL_ID MODEL_ID,
V.POPULAR_NAME POPULAR_NAME,
V.GMC GMC,
V.DISPUTE DISPUTE,
V.ENGINE ENGINE,
'20'|| SUBSTR (V.YEAR, 1, 2)|| ' - '|| '20'|| SUBSTR (V.YEAR, 3, 4) YEAR,
DECODE (V.SUBS, '~', NULL, V.SUBS) SUBS,
V.CONSEC CONSEC,
RFI.CUST_CUST_ID CUST_CUST_ID, 
RFI.SALESMAN SALESMAN
    FROM DEALER.VW_INV_FOR_SALE RFI, DEALER.VW_OLD_SALES P,
    DEALER.VW_NEW_SALES V
        WHERE P.ENGINE_TYPE IN (9,10,11,12,13)
        AND V.MODEL_ID = RFI.MODEL_ID
        AND RFI.MODEL_ID = P.MODEL_ID
        AND V.YEAR >='1011'
        AND V.YEAR = P.MODEL_YEAR
        AND V.ENGINE = P.ENGINE_TYPE
        AND V.SUBS = P.SUBSCRIPT
        AND P.DESCRIPTION != '100% SAFE'
        AND RFI.CUST_CUST_ID IN (SELECT CUST_ID FROM CUSTOMERS WHERE CUST_ID = ?
            UNION 
            SELECT CUST_ID FROM CUSTOMERS 
            START WITH CUST_CUST_ID = ?
            CONNECT BY PRIOR CUST_ID = CUST_CUST_ID)

SELECT DISTINCT
P.CAR_ID CAR_ID,
P.TYPE TYPE,
P.LAST_TIME_SERVICED LAST_TIME_SERVICED,
P.SERVICE_TYPE SERVICE_TYPE,
P.PURCHASE_DATE PURCHASE_DATE,
'20'|| SUBSTR (P.MODEL_YEAR, 1, 2)|| ' - '|| '20'|| SUBSTR (P.MODEL_YEAR, 3, 4) MODEL_YEAR,
RFI.CUST_CUST_ID CUST_CUST_ID, 
RFI.SALESMAN SALESMAN,
P.DESCRIPTION DESCRIPTION,
P.ENGINE_TYPE ENGINE_TYPE,
Q.MODEL_ID MODEL_ID,
Q.POPULAR_NAME POPULAR_NAME,
Q.GMC GMC,
Q.DISPUTE DISPUTE,
Q.ENGINE ENGINE,
'20'|| SUBSTR (Q.YEAR, 1, 2)|| ' - '|| '20'|| SUBSTR (Q.YEAR, 3, 4) YEAR,
DECODE (Q.SUBS, '~', NULL, Q.SUBS) SUBS,
Q.CONSEC CONSEC,
Q.RESULT RESULT
    FROM DEALER.VW_INV_FOR_SALE RFI, DEALER.VW_OLD_SALES P,
    DEALER.VW_ENGINE4_RESULT Q
        WHERE P.ENGINE_TYPE IN (4,44)
        AND Q.MODEL_ID = RFI.MODEL_ID
        AND RFI.MODEL_ID = P.MODEL_ID
        AND Q.YEAR >='1011'
        AND Q.YEAR = P.MODEL_YEAR
        AND Q.ENGINE = P.ENGINE_TYPE
        AND Q.SUBS = P.SUBSCRIPT
        AND P.DESCRIPTION != '100% SAFE'
        AND RFI.CUST_CUST_ID IN (SELECT CUST_ID FROM CUSTOMERS WHERE CUST_ID = ?
            UNION 
            SELECT CUST_ID FROM CUSTOMERS 
            START WITH CUST_CUST_ID = ?
            CONNECT BY PRIOR CUST_ID = CUST_CUST_ID)

非常感谢您对此的任何帮助。

1 个答案:

答案 0 :(得分:0)

实现这一目标的最不具侵入性的方法是简单地UNION两个查询,然后在列上应用ORDER BY进行排序。唯一的问题是UNION要求两个查询中的所有列都排成一行。由于一个查询中的列不在另一个查询中,因此必须用占位符替换它们(通常为NULL)。

这样的事情:

(SELECT DISTINCT
    P.CAR_ID CAR_ID,
    P.TYPE TYPE,
    P.LAST_TIME_SERVICED LAST_TIME_SERVICED,
    P.SERVICE_TYPE SERVICE_TYPE,
    P.PURCHASE_DATE PURCHASE_DATE,
    '20'|| SUBSTR (P.MODEL_YEAR, 1, 2)|| ' - '|| '20'|| SUBSTR (P.MODEL_YEAR, 3, 4) MODEL_YEAR,
    NULL,
    NULL,
    P.DESCRIPTION DESCRIPTION,
    P.ENGINE_TYPE ENGINE_TYPE,
    V.MODEL_ID MODEL_ID,
    V.POPULAR_NAME POPULAR_NAME,
    V.GMC GMC,
    V.DISPUTE DISPUTE,
    V.ENGINE ENGINE,
    '20'|| SUBSTR (V.YEAR, 1, 2)|| ' - '|| '20'|| SUBSTR (V.YEAR, 3, 4) YEAR,
    DECODE (V.SUBS, '~', NULL, V.SUBS) SUBS,
    V.CONSEC CONSEC,
    RFI.CUST_CUST_ID CUST_CUST_ID, 
    RFI.SALESMAN SALESMAN
FROM DEALER.VW_INV_FOR_SALE RFI, DEALER.VW_OLD_SALES P,
    DEALER.VW_NEW_SALES V
WHERE P.ENGINE_TYPE IN (9,10,11,12,13)
    AND V.MODEL_ID = RFI.MODEL_ID
    AND RFI.MODEL_ID = P.MODEL_ID
    AND V.YEAR >='1011'
    AND V.YEAR = P.MODEL_YEAR
    AND V.ENGINE = P.ENGINE_TYPE
    AND V.SUBS = P.SUBSCRIPT
    AND P.DESCRIPTION != '100% SAFE'
    AND RFI.CUST_CUST_ID IN (SELECT CUST_ID FROM CUSTOMERS WHERE CUST_ID = ?
                              UNION 
                             SELECT CUST_ID FROM CUSTOMERS 
                                    START WITH CUST_CUST_ID = ?
                                    CONNECT BY PRIOR CUST_ID = CUST_CUST_ID)
UNION
SELECT DISTINCT
    P.CAR_ID CAR_ID,
    P.TYPE TYPE,
    P.LAST_TIME_SERVICED LAST_TIME_SERVICED,
    P.SERVICE_TYPE SERVICE_TYPE,
    P.PURCHASE_DATE PURCHASE_DATE,
    '20'|| SUBSTR (P.MODEL_YEAR, 1, 2)|| ' - '|| '20'|| SUBSTR (P.MODEL_YEAR, 3, 4) MODEL_YEAR,
    RFI.CUST_CUST_ID CUST_CUST_ID, 
    RFI.SALESMAN SALESMAN,
    P.DESCRIPTION DESCRIPTION,
    P.ENGINE_TYPE ENGINE_TYPE,
    Q.MODEL_ID MODEL_ID,
    Q.POPULAR_NAME POPULAR_NAME,
    Q.GMC GMC,
    Q.DISPUTE DISPUTE,
    Q.ENGINE ENGINE,
    '20'|| SUBSTR (Q.YEAR, 1, 2)|| ' - '|| '20'|| SUBSTR (Q.YEAR, 3, 4) YEAR,
    DECODE (Q.SUBS, '~', NULL, Q.SUBS) SUBS,
    Q.CONSEC CONSEC,
    NULL,   
    Q.RESULT RESULT
FROM DEALER.VW_INV_FOR_SALE RFI, DEALER.VW_OLD_SALES P,
     DEALER.VW_ENGINE4_RESULT Q
WHERE P.ENGINE_TYPE IN (4,44)
    AND Q.MODEL_ID = RFI.MODEL_ID
    AND RFI.MODEL_ID = P.MODEL_ID
    AND Q.YEAR >='1011'
    AND Q.YEAR = P.MODEL_YEAR
    AND Q.ENGINE = P.ENGINE_TYPE
    AND Q.SUBS = P.SUBSCRIPT
    AND P.DESCRIPTION != '100% SAFE'
    AND RFI.CUST_CUST_ID IN (SELECT CUST_ID FROM CUSTOMERS WHERE CUST_ID = ?
                              UNION 
                             SELECT CUST_ID FROM CUSTOMERS 
                                    START WITH CUST_CUST_ID = ?
                                    CONNECT BY PRIOR CUST_ID = CUST_CUST_ID) )
ORDER BY 1,2,3