簡體   English   中英

SQL Oracle /聯接兩個查詢

[英]SQL Oracle/Joining two queries

我有兩個查詢需要組合在一起,但不確定如何…。

第一個查詢從帳戶表,對帳金額,期間和任何沖銷的金額(如果有)中提取任何選定帳戶的最后三個對帳金額

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

我想做的是有一個查詢,它查詢所有感興趣的帳戶(按照第二個查詢)的最后三個對帳金額(按照第一個查詢); 但是,我無法將兩者合並到單個查詢中……

在with子句中將第一個查詢添加為另一個查詢集,然后對其進行INNER JOIN 。由於以任何方式進行分組,因此在最終選擇中也可能不需要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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM