2017-10-18 90 views
0

我有兩個需要縫合在一起的查詢,但我不知道如何...。SQL Oracle /加入兩個查詢

該第一查詢通過最後三個協調量從帳戶,覈對金額,週期表中的任何選擇的帳戶拉動,並寫入任何量關閉(如果有的話)

SELECT * 
FROM  (
SELECT * 
FROM (
     SELECT gwod.account_id, 
       EXTRACT(month FROM gwod.charge_period_start) charge_period_month, 
       SUM(gwod.total_due_on_charge) total_due_on_charge, 
       SUM(gwod.amount_written_off) amount_written_off, 
       DENSE_RANK() over (PARTITION BY gwod.account_id 
     ORDER BY EXTRACT(month FROM 
     gwod.charge_period_start) DESC) rownumber 
     FROM  Accounts_report gwod 
     WHERE account_id IN ('') 
     GROUP BY gwod.account_id, 
       EXTRACT(month FROM gwod.charge_period_start) 
     HAVING SUM (gwod.total_due_on_charge) <> 0) t1 
WHERE t1.rownumber <=3) 
PIVOT (MAX(charge_period_month) charge_period, 
     MAX(total_due_on_charge) total_due_on_charge, 
     MAX(amount_written_off) amount_written_off 
     FOR rownumber IN (1,2,3)) 
ORDER BY account_id 

該查詢基本上得到我,我很感興趣,一些額外的表帳戶列表...

WITH Account_Owners AS 
      (select gs.account_id, AP.SUPERVISOR 
      from Account_Info gs 
      Left join ACC_OWNERS AD 
      On gs.account_id = AD.ACCOUNT_NUMBER 
      Left Join Onwers_Info AP 
      On ad.owned_by = AP.ADNAME 
      group by account_id, AP.SUPERVISOR 
     ) 

SELECT distinct POLICY_INFO.ACCOUNT_ID, Count (POLICY_INFO.POLICY_NO) As 
Active, a.supervisor 
FROM POLICY_INFO 
inner join Account_owners a on policy_info.account_id = a.account_id 

WHERE Policy_Info.POLICY_STATUS = 'Active' 
And policy_info.ACCOUNT_ID is not Null 
And a.supervisor in ('David Smith') 
GROUP BY Policy_Info.ACCOUNT_ID, a.supervisor 
ORDER BY Policy_Info.ACCOUNT_ID 

我想要做的是有一個查詢它通過最後三個協調拉動金額(按第一個查詢)爲我的所有帳戶nterest(根據第二個查詢);我在兩者結合成然而然而單查詢的麻煩......

回答

0

添加第一個查詢作爲另一組在與子句和INNER JOIN it.Also DISTINCT可能無法在最後的選擇,你是分組任何方式要求。試試這個,讓我知道它是否正確,因爲我很難用查詢來查看數據。

WITH Account_charges AS 
    (
    SELECT * 
    FROM  (
    SELECT * 
    FROM (
      SELECT gwod.account_id, 
        EXTRACT(month FROM gwod.charge_period_start) charge_period_month, 
        SUM(gwod.total_due_on_charge) total_due_on_charge, 
        SUM(gwod.amount_written_off) amount_written_off, 
        DENSE_RANK() over (PARTITION BY gwod.account_id 
             ORDER BY EXTRACT(month FROM gwod.charge_period_start) DESC) rownumber 
      FROM  Accounts_report gwod 
      WHERE account_id IN ('') 
      GROUP BY gwod.account_id, 
        EXTRACT(month FROM gwod.charge_period_start) 
      HAVING SUM (gwod.total_due_on_charge) <> 0) t1 
    WHERE t1.rownumber <=3) 
    PIVOT (MAX(charge_period_month) charge_period, 
      MAX(total_due_on_charge) total_due_on_charge, 
      MAX(amount_written_off) amount_written_off 
      FOR rownumber IN (1,2,3)) 
      ), 
      Account_Owners AS 
       (select gs.account_id, AP.SUPERVISOR 
       from Account_Info gs 
       Left join ACC_OWNERS AD 
       On gs.account_id = AD.ACCOUNT_NUMBER 
       Left Join Onwers_Info AP 
       On ad.owned_by = AP.ADNAME 
       group by account_id, AP.SUPERVISOR 
      ) 
       SELECT distinct POLICY_INFO.ACCOUNT_ID, Count (POLICY_INFO.POLICY_NO) As 
    Active, a.supervisor ,MAX(b.charge_period),MAX(b.total_due_on_charge),MAX(b.amount_written_off) 

--use the proper column names. 
    FROM POLICY_INFO 
    inner join Account_owners a on policy_info.account_id = a.account_id 
    INNER JOIN Account_charges b ON policy_info.account_id = b.account_id 

    Where Policy_Info.POLICY_STATUS = 'Active' 
    And policy_info.ACCOUNT_ID is not Null 
    And a.supervisor in ('David Smith') 
    Group by Policy_Info.ACCOUNT_ID, a.supervisor 
    order by Policy_Info.ACCOUNT_ID; 
+0

非常感謝,它的工作原理,但它給我的輸出作爲第二個查詢一樣,我不是知道如何修改,以便它給我相同的輸出作爲第一個查詢,所以下面列; Account_ID,1_Charge_Period,1_Total_DUE_ON_CHARGE,1_AMOUNT_WRITTEN_OFF,2_Charge_Period,2_Total_DUE_ON_CHARGE,2_AMOUNT_WRITTEN_OFF,3_Charge_Period,3_Total_DUE_ON_CHARGE,3_AMOUNT_WRITTEN_OFF。我需要查看所有從第二個查詢中獲取的帳戶信息。希望這一切都有道理。 –

+0

好的。然後在with子句中只取上面'PIVOT'塊的上面部分,並在最終查詢中添加你的PIVOT ..? –