合并多个查询的输出

时间:2019-02-07 10:30:40

标签: sql oracle union-all

我一直试图将两个具有相同字段的查询的输出合并在一起,我已经看过人们以前在类似问题和答案中提供的几个示例,但似乎无法让它们为我工作。

我有两个查询,它们基本上提供 Account_ID,Specific_Transaction_Type_Amount,Transaction_Date 作为第一个查询的输出,然后第二个查询输出 Account_ID,Account_Value

第二个查询有点复杂,因为它必须查找一个帐户商品ID,该商品的最新价格日期,该价格日期的价格,然后将该商品的数量相加,乘以商品的价格按帐户ID分组的项目。

所以第一个查询看起来像这样:

Select distinct
ACCOUNT_NUMBER
,TrANSACTION_AMOUNT
,TRANSACTION_DATE
from CLIENT CL
left join
CLIENT_ACCOUNT_LINK CAL on CL.CLIENT_ID = CAL.CAL_CLIENT_ID
left join
ACCOUNT A on CAL.CAL_ACCOUNT_ID = A.ACCOUNT_ID
left join
ACCOUNT_TYPE AT on A.ACCOUNT_TYPE_ID = AT.ACCOUNT_TYPE_ID
left join
ACCOUNT_TRANSACTION ATR on A.ACCOUNT_ID = ATR.ACCOUNT_ID
WHERE 
AT.ACCOUNT_TYPE = 'ACCOUNTYPE2'
AND ATR.TRANSACTION_TYPE = 'TRANSACTIONTYPE6'

第二个查询看起来像这样:

select distinct
A.ACCOUNT_NUMBER
,SUM(AB.ITEM_QTY * PR.ITEM_PRICE) OVER (PARTITION BY A.ACCOUNT_ID) as                     TotalValue
from ACCOUNT A 
join ACCOUNT_BALANCE AB on A.ACCOUNT_ID = AB.ACCOUNT_ID
join ITEM_LIST IL on AB.ITEM_ID = IL.ITEM_ID
join PRICE_DATE PD on IL.ITEM_ID= PD.ITEM_ID
join CODE_LOOKUP cODE on PD.PRICE_STATUS_CODE = CODE.CODE_ID AND CODE.DESCRIPTION = 'ACTIVE'
AND PD.PRICE_DATE = (SELECT MAX(PD2.PRICE_DATE)
FROM PRICE_DATE PD2
WHERE PD2.ITEM_ID = PD.ITEM_ID
AND PD2.PRICE_STATUS_CODE = CODE.CODE_ID
AND sprcp.prcp_effective_date < sysdate + 1)
join PRICE PR on PD.PRICE_ID = PRICE_PRICE_DATE_ID
join PRICE_TYPE PT on PR.PRICE_TYPE_ID = PT.PRICE_TYPE_ID

所以我得到两个结果:

ACCOUNT_ID    TRANSACTION_AMOUNT    TRANSACTION_DATE
_____________________________________________________
123456789      500.23                 07/02/2019

ACCOUNT_ID    TOTAL_VALUE
____________________________
123456789      24365.48

我希望查询2中的TOTAL_VALUE列出现在查询1中。如果将两个查询合并为一个,该查询将运行很长时间,直到出现内存不足错误。如果我分别运行它们,它们每个都需要花费几分钟,但是我得到的结果没有任何问题。

我确实根据先前的示例进行了以下尝试:

SELECT *
FROM (QUERY1) AS Q1
LEFT JOIN (QUERY2) AS Q2
on Q1.A.ACCOUNT_ID = Q2.A.ACCOUNT_ID

当我尝试运行此查询时出现语法错误,特别是它似乎不喜欢查询别名吗?

1 个答案:

答案 0 :(得分:2)

尝试

with 
q2 (select distinct
            A.ACCOUNT_NUMBER
            ,SUM(AB.ITEM_QTY * PR.ITEM_PRICE) OVER (PARTITION BY A.ACCOUNT_ID) as                     TotalValue
            from ACCOUNT A 
            join ACCOUNT_BALANCE AB on A.ACCOUNT_ID = AB.ACCOUNT_ID
            join ITEM_LIST IL on AB.ITEM_ID = IL.ITEM_ID
            join PRICE_DATE PD on IL.ITEM_ID= PD.ITEM_ID
            join CODE_LOOKUP cODE on PD.PRICE_STATUS_CODE = CODE.CODE_ID AND CODE.DESCRIPTION = 'ACTIVE'
            AND PD.PRICE_DATE = (SELECT MAX(PD2.PRICE_DATE)
            FROM PRICE_DATE PD2
            WHERE PD2.ITEM_ID = PD.ITEM_ID
            AND PD2.PRICE_STATUS_CODE = CODE.CODE_ID
            AND sprcp.prcp_effective_date < sysdate + 1)
            join PRICE PR on PD.PRICE_ID = PRICE_PRICE_DATE_ID
            join PRICE_TYPE PT on PR.PRICE_TYPE_ID = PT.PRICE_TYPE_ID),
    q1 as (Select distinct
                ACCOUNT_NUMBER
                ,TrANSACTION_AMOUNT
                ,TRANSACTION_DATE
                from CLIENT CL
                left join
                CLIENT_ACCOUNT_LINK CAL on CL.CLIENT_ID = CAL.CAL_CLIENT_ID
                left join
                ACCOUNT A on CAL.CAL_ACCOUNT_ID = A.ACCOUNT_ID
                left join
                ACCOUNT_TYPE AT on A.ACCOUNT_TYPE_ID = AT.ACCOUNT_TYPE_ID
                left join
                ACCOUNT_TRANSACTION ATR on A.ACCOUNT_ID = ATR.ACCOUNT_ID
                WHERE 
                AT.ACCOUNT_TYPE = 'ACCOUNTYPE2'
                AND ATR.TRANSACTION_TYPE = 'TRANSACTIONTYPE6')
        select * from q1
          join q2  on q1.account_number = q2.account_number