2015-12-17 94 views
0

我有2個工作集的子查詢。我試圖讓他們到一個查詢這些字段:加入多個子查詢Oracle

select tos.source_system_order_id , tos.TOS_Date, tos.TOS_Final_Charge_Amt_Sum , oes.OES_Final_Charge_Amt_Sum 

第一個查詢:

SELECT tos1.source_system_order_id, 
     tos1.tos_date, 
     SUM(tos1.tos_final_charge_amt_sum) 
FROM (SELECT source_system_order_id, 
       source_system_cd, 
       To_char(billing_month_dt, 'YYYYMM') AS TOS_Date, 
       tos_final_charge_amt_sum 
     FROM tl_ov_stage 
     ORDER BY source_system_order_id) TOS1 
GROUP BY tos1.source_system_order_id, 
      tos1.tos_date 

2ndquery

SELECT OES1.source_system_order_id, 
     oes1.oes_date, 
     SUM(oes1.oes_final_charge_amt_sum) AS OES_Final_Charge_Amt_Sum 
FROM (SELECT To_char("date", 'YYYYMM') AS OES_Date, 
       To_char("service order id") AS SOURCE_SYSTEM_ORDER_ID, 
       oes_final_charge_amt_sum 
     FROM [email protected]_link) OES1 
GROUP BY OES1.source_system_order_id, 
      oes1.oes_date, 
      oes1.order_status 

回答

1

嘗試使用CTE將二者結合起來select查詢。我發現CTE在這種情況下更具可讀性

with tos 
as 
(
SELECT tos1.source_system_order_id, 
     tos1.tos_date, 
     SUM(tos1.tos_final_charge_amt_sum) 
FROM (SELECT source_system_order_id, 
       source_system_cd, 
       To_char(billing_month_dt, 'YYYYMM') AS TOS_Date, 
       tos_final_charge_amt_sum 
     FROM tl_ov_stage 
     ORDER BY source_system_order_id) TOS1 
GROUP BY tos1.source_system_order_id, 
      tos1.tos_date 
), 
OES as 
(
SELECT OES1.source_system_order_id, 
     oes1.oes_date, 
     SUM(oes1.oes_final_charge_amt_sum) AS OES_Final_Charge_Amt_Sum 
FROM (SELECT To_char("date", 'YYYYMM') AS OES_Date, 
       To_char("service order id") AS SOURCE_SYSTEM_ORDER_ID, 
       oes_final_charge_amt_sum 
     FROM [email protected]_link) OES1 
GROUP BY OES1.source_system_order_id, 
      oes1.oes_date, 
      oes1.order_status 
) 
select tos.source_system_order_id, 
     tos.TOS_Date, 
     tos.TOS_Final_Charge_Amt_Sum, 
     oes.OES_Final_Charge_Amt_Sum 
from tos 
inner join oes 
     on tos.source_system_order_id = oes.source_system_order_id 
     AND tos.tos_date = oes.oes_date -- Remove if this is not needed 
+0

我喜歡這種方法。但我得到了與以前的嘗試相同的錯誤。 「SQL命令沒有正確結束。」紅色swiggle在這個聲明中「從」cte1作爲tos「 –

+0

@GET_Amazed - 檢查現在更新。用oracle語法不好 –

+0

VR46你可以看看我的另一個查詢嗎? –