2017-08-23 148 views
-1

這是我的草稿代碼,它將根據放置位置,括號的位置以及爲每個表分配名稱而失敗。我不確定如何在加入多個表時創建具有左連接的嵌套查詢。左加入和多表的子查詢

任何有關如何重寫下面的代碼的建議非常感謝。

在此先感謝您的幫助!

IF OBJECT_ID('tempdb..#1') IS NOT NULL DROP TABLE #1 ; 
IF OBJECT_ID('tempdb..#2') IS NOT NULL DROP TABLE #2 ; 
IF OBJECT_ID('tempdb..#3') IS NOT NULL DROP TABLE #3 ; 


SELECT * FROM nwh_analytics.dbo.km_cmr_table a 

left join (
select 
    b.prov_id + b.prov_site_num as prov, 
    count(distinct b.claim_num) total_claims_billed, 
    sum(b.paid) as total_dollars_paid 

into  
#1 -- drop table #1 

from  
datawarehouse.dbo.claims b 
where 
dos >= '2017-01-01' 
and fee_sched like 'tup%' 
and a.code = b.CPT_CODE 

group by  
    prov_id + prov_site_num) 

    on a.code = b.CPT_CODE 


Select * From nwh_analytics.dbo.km_cmr_table a 

left join(
select 
d.contract_id, 
d.contract_legal_name, 
b.prov_id + b.prov_site_num as prov, 
c.prov_full_name, 
count(distinct b.claim_num) as claims_with_new_codes, 
sum(b.paid) as paid_for_claims_with_new_codes, 
e.total_claims_billed, 
e.total_dollars_paid 

into  
#2 

from  
datawarehouse.dbo.claims b 
join datawarehouse.dbo.providers c 
on b.prov_id = c.prov_id 
join datawarehouse.dbo.providers_site d 
on b.prov_id + b.prov_site_num =c.prov_id + d.site_num 
join #1 e 
on b.prov_id + b.prov_site_num = e.prov 

where 
b.dos >= '2017-01-01' 
and b.fee_sched like 'tup%') 
a.code = b. FEE_CPT 



Select * From nwh_analytics.dbo.km_cmr_table a 

left join(select  
c.contract_id, 
c.contract_legal_name, 
a.prov_id + a.prov_site_num as prov, 
b.prov_full_name, 
a.claim_num, 
a.line_num, 
a.dos, 
a.cpt_code, 
cd.cpt_desc, 
a.proc_mod1, 
a.proc_mod2, 
a.billed, 
a.allowed, 
a.paid, 
a.deny_code, 
a.paid_date, 
a.Fee_Sched 

into  
#3 

from  
    datawarehouse.dbo.claims a 
    join datawarehouse.dbo.providers b 
    on a.prov_id = b.prov_id 
    join datawarehouse.dbo.providers_site c 
    on a.prov_id + a.prov_site_num = c.prov_id + c.site_num 
    join datawarehouse.dbo.cpt_codes cd 
    on a.cpt_code = cd.cpt_cd 

    where 
    a.dos >= '2017-01-01' 
    and a.fee_sched like 'tup%') 
    on a.Code = b. Fee_CPT 
+0

你想用'INTO#n'子句做什麼?您不能將SELECT INTO作爲子查詢(或派生表)執行到外部SELECT。 –

+0

感謝您的回覆。我正在創建臨時表並將其刪除。我不知道如何解決這個問題。 – Kimm

+0

你沒有解釋你想要做什麼,你只是給出了一些無法解釋的破壞代碼,不能做你想做的事情。您甚至不清楚您所需過程的輸入和輸出是什麼。請編輯清楚。請閱讀並在[mcve]上採取行動。 – philipxy

回答

0

如果你只是想創建TEMP表,那麼你只需要重新排列這樣的查詢。我將使用第一個作爲一個例子,你可以使用同樣的邏輯別人:

SELECT * 
into #1 -- drop table #1 
FROM nwh_analytics.dbo.km_cmr_table a 
left join (
select 
    CPT_CODE, 
    prov_id + prov_site_num as prov, 
    count(distinct claim_num) total_claims_billed, 
    sum(paid) as total_dollars_paid 
from  
datawarehouse.dbo.claims 
where 
dos >= '2017-01-01' 
and fee_sched like 'tup%' 
group by  
    CPT_CODE, prov_id + prov_site_num) b 
    on a.code = b.CPT_CODE 
+0

謝謝。有些東西不起作用,它可能是表名和左括號結束後的括號之內和之後的別名? – Kimm

+0

如果兩個字段具有相同的名稱,則無法選擇臨時表。這是可能的嗎?順便說一句,你永遠不應該使用select *當你有一個連接,因爲你正在返回重複的數據,並影響性能。另外你會看到你是否有兩個同名的列。如果不是這樣,那麼你用這個代碼得到的錯誤是什麼? – HLGEM

+0

這是子查詢的where子句中的a-b條件。我將它移動到JOIN條件。試試我目前的代碼。 (實際上我刪除它,因爲它已經處於JOIN條件,所以它在WHERE子句中只是多餘的。)等待還有一個問題。 –

0

我注意到,您正在使用從子查詢表的別名b使用a.code = B連接。 CPT_CODE。你可以直接從子查詢中取出cpt_code。

SELECT * 
    FROM nwh_analytics.dbo.km_cmr_table a 
    LEFT JOIN (SELECT cpt_code, 
        prov_id + b.prov_site_num AS prov, 
        COUNT(DISTINCT claim_num) total_claims_billed, 
        SUM(paid) AS total_dollars_paid 
       INTO #1 
       FROM datawarehouse.dbo.claims 
       WHERE dos >= '2017-01-01' 
       AND fee_sched like 'tup%' 
       GROUP BY cpt_code, 
         prov_id + prov_site_num) b 
    ON a.code = b.cpt_code  
+0

我不確定我關注? – Kimm

+0

忽略,是的這個作品謝謝你! – Kimm

+0

我想編輯你的#2和#3,但我不知道哪張表fee_cpt來自。 –