2017-10-18 96 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 ..? –