左连接和具有多个表的子查询

时间:2017-08-23 17:51:00

标签: sql sql-server subquery left-join

这是我的草稿代码,它将根据展示位置,括号的位置以及为每个表分配名称而失败。我不确定如何在连接多个表时使用左连接创建嵌套查询。

非常感谢有关如何重写以下代码的任何建议。

提前感谢您的帮助!

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

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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