仅将两个SELECT合并为一个

时间:2017-11-29 08:31:04

标签: sql sql-server

我需要将这两个查询合并为仅有4列。

查询1:

    SQL> grant execute on utl_http to username;

    Grant succeeded.

提前致谢。

3 个答案:

答案 0 :(得分:1)

为什么MKP_Number在两个查询中有所不同?是不是?

作为变体,您可以尝试使用FULL JOIN

SELECT
    ISNULL(q1.MKP_Number,q2.MKP_Number) MKP_Number,
    q1.PC_Less_30dB,
    q1.PC_More_30dB,
    q2.CL_Less_3_5_Years,
    q2.CL_More_3_5_Years
FROM
    (
        SELECT
            MKP_Number,
            CASE WHEN  ( ( [MKP_LastHearTestFletcherIndexLeft] < 30 ) OR ([MKP_LastHearTestFletcherIndexRight] < 30) )
                THEN 'PC_Less_30dB'
            END AS PC_Less_30dB,
            CASE WHEN  ( ( [MKP_LastHearTestFletcherIndexLeft] > 30 ) OR ([MKP_LastHearTestFletcherIndexRight] > 30) )
                THEN 'PC_More_30dB'
            END AS PC_More_30dB
        FROM RPT.ANIDIM_MarketingPerson
        WHERE ([MKP_LastSalesHEADate] IS NULL)
        AND MKP_Number =5830656
    ) q1
FULL JOIN
    (
        SELECT
            MKP_Number,
            CASE WHEN  ( Mkp_Lastsalesheadaysago<1278 )
                THEN 'CL_Less_3_5_Years'
            END AS CL_Less_3_5_Years,
             CASE WHEN  ( Mkp_Lastsalesheadaysago>1277 )
                THEN 'CL_More_3_5_Years'
            END AS CL_More_3_5_Years
        FROM RPT.ANIDIM_MarketingPerson
        WHERE ([MKP_LastSalesHEADate] IS NOT NULL)
        AND MKP_Number =2098914
    ) q2
ON q1.MKP_Number=q2.MKP_Number

三个子查询

SELECT
    COALESCE(q1.MKP_Number,q2.MKP_Number,q3.MKP_Number) MKP_Number, -- !!!
    q1.PC_Less_30dB,
    q1.PC_More_30dB,
    q2.CL_Less_3_5_Years,
    q2.CL_More_3_5_Years,
    q3.CL_Asl -- !!!
FROM
    (
        SELECT
            MKP_Number,
            CASE WHEN  ( ( [MKP_LastHearTestFletcherIndexLeft] < 30 ) OR ([MKP_LastHearTestFletcherIndexRight] < 30) )
                THEN 'PC_Less_30dB'
            END AS PC_Less_30dB,
            CASE WHEN  ( ( [MKP_LastHearTestFletcherIndexLeft] > 30 ) OR ([MKP_LastHearTestFletcherIndexRight] > 30) )
                THEN 'PC_More_30dB'
            END AS PC_More_30dB
        FROM RPT.ANIDIM_MarketingPerson
        WHERE ([MKP_LastSalesHEADate] IS NULL)
        AND MKP_Number =5830656
    ) q1
FULL JOIN
    (
        SELECT
            MKP_Number,
            CASE WHEN  ( Mkp_Lastsalesheadaysago<1278 )
                THEN 'CL_Less_3_5_Years'
            END AS CL_Less_3_5_Years,
             CASE WHEN  ( Mkp_Lastsalesheadaysago>1277 )
                THEN 'CL_More_3_5_Years'
            END AS CL_More_3_5_Years
        FROM RPT.ANIDIM_MarketingPerson
        WHERE ([MKP_LastSalesHEADate] IS NOT NULL)
        AND MKP_Number =2098914
    ) q2
ON q1.MKP_Number=q2.MKP_Number
FULL JOIN
  (
    SELECT
      MKP_Number,
      CASE WHEN ( Mkp_Lastsalesheacustomercontributionamount != 0 ) AND (Mkp_Lastsalesheainsuranceamount != 0 )
        THEN 'CL_Asl'
      END AS CL_Asl
    FROM RPT.ANIDIM_MarketingPerson
    WHERE ([MKP_LastSalesHEADate] IS NOT NULL) AND MKP_Number =5338443  
  ) q3
ON q3.MKP_Number=ISNULL(q1.MKP_Number,q2.MKP_Number)

我认为以下变体会更好

SELECT
    MKP_Number,

    CASE WHEN ([MKP_LastSalesHEADate] IS NULL AND MKP_Number =5830656)
        AND ([MKP_LastHearTestFletcherIndexLeft] < 30  OR [MKP_LastHearTestFletcherIndexRight] < 30) THEN 'PC_Less_30dB'
    END AS PC_Less_30dB,

    CASE WHEN ([MKP_LastSalesHEADate] IS NULL AND MKP_Number =5830656)
        AND ([MKP_LastHearTestFletcherIndexLeft] > 30  OR [MKP_LastHearTestFletcherIndexRight]) > 30 THEN 'PC_More_30dB'
    END AS PC_More_30dB,

    CASE WHEN ([MKP_LastSalesHEADate] IS NOT NULL AND MKP_Number =2098914)
        AND (Mkp_Lastsalesheadaysago<1278) THEN 'CL_Less_3_5_Years'
    END AS CL_Less_3_5_Years,

    CASE WHEN ([MKP_LastSalesHEADate] IS NOT NULL AND MKP_Number =2098914)
        AND (Mkp_Lastsalesheadaysago>1277) THEN 'CL_More_3_5_Years'
    END AS CL_More_3_5_Years,

    CASE WHEN ([MKP_LastSalesHEADate] IS NOT NULL AND MKP_Number =5338443)
      AND (Mkp_Lastsalesheacustomercontributionamount != 0 AND Mkp_Lastsalesheainsuranceamount != 0) THEN 'CL_Asl'
    END AS CL_Asl
FROM RPT.ANIDIM_MarketingPerson
WHERE ([MKP_LastSalesHEADate] IS NULL AND MKP_Number =5830656)
   OR ([MKP_LastSalesHEADate] IS NOT NULL AND MKP_Number =2098914)
   OR ([MKP_LastSalesHEADate] IS NOT NULL AND MKP_Number =5338443)

答案 1 :(得分:0)

您可以使用UNION合并查询结果,但每个查询的结果必须包含相同的列。

在以下示例中,为每个查询中的不相关列设置了NULL值,并添加了假Result_Type列,以便您知道每个结果来自哪个查询。

SELECT 
    CASE WHEN  ( ( [MKP_LastHearTestFletcherIndexLeft] < 30 ) OR ([MKP_LastHearTestFletcherIndexRight] < 30) )
        THEN 'PC_Less_30dB'
        END AS PC_Less_30dB,
    CASE WHEN  ( ( [MKP_LastHearTestFletcherIndexLeft] > 30 ) OR ([MKP_LastHearTestFletcherIndexRight] > 30) )
        THEN 'PC_More_30dB'
        END AS PC_More_30dB,
    NULL AS CL_Less_3_5_Years,  
    NULL AS CL_More_3_5_Years,
    'PC' AS Result_Type
    FROM RPT.ANIDIM_MarketingPerson
    WHERE ([MKP_LastSalesHEADate] IS NULL)
    AND MKP_Number =5830656
UNION ALL           
SELECT 
    CASE WHEN  ( Mkp_Lastsalesheadaysago<1278 )
        THEN 'CL_Less_3_5_Years'
        END AS CL_Less_3_5_Years,
     CASE WHEN  ( Mkp_Lastsalesheadaysago>1277 )
        THEN 'CL_More_3_5_Years'
        END AS CL_More_3_5_Years,
    NULL AS PC_Less_30dB,
    NULL AS PC_More_30dB,
    'CL' AS Result_Type
    FROM RPT.ANIDIM_MarketingPerson
    WHERE ([MKP_LastSalesHEADate] IS NOT NULL)
    AND MKP_Number =2098914

答案 2 :(得分:0)

试试这个:

SELECT  
T1.PC_Less_30dB,T1.PC_More_30dB,T2.CL_Less_3_5_Years,T2.CL_More_3_5_Years  
FROM   
(SELECT   
CASE WHEN  ( ( [MKP_LastHearTestFletcherIndexLeft] < 30 ) OR        ([MKP_LastHearTestFletcherIndexRight] < 30) )  
    THEN 'PC_Less_30dB'  
    END AS PC_Less_30dB,  
CASE WHEN  ( ( [MKP_LastHearTestFletcherIndexLeft] > 30 ) OR   ([MKP_LastHearTestFletcherIndexRight] > 30) )  
    THEN 'PC_More_30dB'  
    END AS PC_More_30dB  
    FROM RPT.ANIDIM_MarketingPerson  
    WHERE ([MKP_LastSalesHEADate] IS NULL)  
AND MKP_Number =5830656) as T1,  
(SELECT   
CASE WHEN  ( Mkp_Lastsalesheadaysago<1278 )  
    THEN 'CL_Less_3_5_Years'  
    END AS CL_Less_3_5_Years,  
CASE WHEN  ( Mkp_Lastsalesheadaysago>1277 )  
    THEN 'CL_More_3_5_Years'  
    END AS CL_More_3_5_Years  
FROM RPT.ANIDIM_MarketingPerson  
WHERE ([MKP_LastSalesHEADate] IS NOT NULL)  
AND MKP_Number =2098914) as T2