SQL Oracle /加入两个查询

时间:2017-10-18 15:00:20

标签: 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

我想要做的是让一个查询通过所有感兴趣的帐户(根据第二个查询)提取最后三个对帐金额(根据第一个查询);然而,我无法将这两者合并到单个查询中...

1 个答案:

答案 0 :(得分:0)

将第一个查询添加为with子句中的另一个集合,INNER JOIN it。在最终选择中可能不需要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;