Multi-part identifier could not be bound?

时间:2016-04-04 16:40:21

标签: sql sql-server

drop table #test1
/*** Master Validation Query!!!!!DO NOT CHANGE GROUPINGS!!!!**/
/**07/25/2007 
1. Updated Discount code to link back to worktable
2. Updated U&C Calculation
3. Updated Non_covered calculation


*********************************************/
select
min(clm_net) as ProNet,
min(v1.val_opt) as opt, 
min(clm_prod) as prod,
min(c.clm_drgc) as drgcode,
min(c.clm_4) as clm_4, 
min(c.clm_cc2) as clm_cc2,
min(c.clm_cc3) as clm_cc3,
min(c.clm_3) as clm_3,
min(c.clm_38a) as clm_38a,
min(c.clm_38b) as clm_38b,
min(c.clm_12a) as clm_12a,
min(c.clm_12b) as clm_12b,
min(c.clm_60a) as clm_60a,
min(m.mem_altid) as AltId,
min(c.clm_5) as clm_5,
min(c.clm_1a1) as clm_1a1 ,
min(c.clm_1a2) as clm_1a2,
min(c.clm_deg) as clm_deg,
min(pro_spec1) as specialty,
min(v3.val_desc) as ProDesc1,
min(pro_xtyp) as protype,
min(c.clm_1a) as clm_1a,
min(c.clm_batch) as clm_batch,
min(c.clm_id1) as clm_id1,
min(s.clms_line) as clms_line,
min(s.clms_from) as clms_from,
min(s.clms_thru) as clms_thru,
impact.dbo.u_poscalc3(min(clm_form),min(clm_4),min(clms_rev),min(clm_spc),min(clms_pos),min(clms_ben),min(clm_67)) as clm_POS,
min(s.clms_rev) as clms_rev,
min(v6.val_desc) as procdesc2,
min(s.clms_proc) as clms_proc,
min(v4.val_desc) as procdesc,
min(s.clms_mod1) as clms_mod1,   
min(v5.val_desc) as moddesc,
min(c.clm_67) as Diagnosis,
min(v1.val_desc) as Dcode,
min(v1.val_opt) as class,
min(v2.val_desc) as Dclass,
min(c.clm_68) as clm_68,
min(c.clm_69) as clm_69,
min(s.clms_chg) as clms_chg,
min(s.clms_sku) as clms_sku,
min(s.clms_allow) as clms_allow,
min(s.clms_copa) as clms_copa,
min(s.clms_dedu) as clms_dedu,
min(s.clms_coin) as clms_coin,         
min(s.clms_non) as clms_non,
min(s.clms_cobp) as clms_cobp,
min(s.clms_wh) as clms_wh,   
min(s.clms_payp) as clms_payp,
min(s.clms_paye) as clms_paye,
min(c.clm_inet) as clm_inet,
min(s.clms_ben) as clms_ben,
min(c.clm_form) as clm_form,
Min(c.clm_stades) as clm_stades,
min(c.clm_edid) as clm_edid,
Min(c.clm_chkno) as clm_chkno,
min(c.clm_dout) as clm_dout,
min(c.clm_chgdt) as clm_chgdt,
min(c.clm_ips) as clm_ips,
min(c.clm_den) as Den,
min(clm_pclm) as TXENnum,
min(d.clmd_cm01) as prindiag,
min(icd10.DIAG_CODE) as diagcode
into #test1
from impact.dbo.tbl_clm c
left join impact.dbo.tbl_mem m on m.mem_id1 = c.clm_12
left join impactwork.dbo.icd_diag_codes icd9 on icd9.diag_code =                 left(c.clm_67, 3) and icd9.code_type = 'ICD9_DIAG'
left join impactwork.dbo.icd_diag_codes icd10 on icd10.diag_code =      left(d.clmd_cm01, 3) and icd10.code_type = 'ICD10_DIAG'
left join impact.dbo.tbl_clmd d on clmd_id1 = c.clm_id1
left join impact.dbo.tbl_meme e on e.meme_id1 = c.clm_12
left join impact.dbo.tbl_clms s on s.clms_id = c.clm_id1
left join impact.dbo.tbl_valid diag on diag.val_code = clm_67 and         diag.val_type = '503'
left join tbl_valid v1 on v1.val_type = '57a'and v1.val_code = c.clm_67       --primary diagnosis
left join tbl_valid v2 on v2.val_type = '57b' and v2.val_code = v1.val_opt    --      The val_opt of the code relates to the class
left join impact.dbo.tbl_pro p  on c.clm_1 = p.pro_id1
left join tbl_valid v3 on v3.val_type = '302'and v3.val_code = pro_spec1
left join tbl_valid v4 on v4.val_type = '501'  and v4.val_code = clms_proc 
left join tbl_valid v5 on v5.val_type = '502'  and v5.val_code = clms_mod1
left join tbl_valid v6 on v6.val_type = '506'  and v6.val_code = clms_rev
where c.clm_stades = 'paid' 
and c.clm_stades != 'dupl'
and clm_form != 'B'
and clms_Actn != 'Change'           
and c.clm_prod  !='den'
and c.clm_dout between '01/01/2015' and '05/31/2015' 
and c.clm_prod <> 'flx'
and clm_cc2 in ('52037')
group by c.clm_id1,s.clms_line,c.clm_cc2
order by c.clm_Id1,s.clms_line,c.clm_cc2

select *

from #test1

I am getting an error:

on line 85 stating, 'The multi-part identifier "d.clmd_cm01" could not be bound.

Why am I getting such an error? I looked to make sure that all of my aliases are used in the join, but that doesn't seem to be the problem.

2 个答案:

答案 0 :(得分:4)

You need to have the join:

left join impact.dbo.tbl_clmd d on clmd_id1 = c.clm_id1

before

left join impactwork.dbo.icd_diag_codes icd10 on icd10.diag_code =      left(d.clmd_cm01, 3) and icd10.code_type = 'ICD10_DIAG'

As the above join uses a column from impact.dbo.tbl_clmd d as a join predicate.

This should work:

drop table #test1
/*** Master Validation Query!!!!!DO NOT CHANGE GROUPINGS!!!!**/
/**07/25/2007 
1. Updated Discount code to link back to worktable
2. Updated U&C Calculation
3. Updated Non_covered calculation


*********************************************/
select
min(clm_net) as ProNet,
min(v1.val_opt) as opt, 
min(clm_prod) as prod,
min(c.clm_drgc) as drgcode,
min(c.clm_4) as clm_4, 
min(c.clm_cc2) as clm_cc2,
min(c.clm_cc3) as clm_cc3,
min(c.clm_3) as clm_3,
min(c.clm_38a) as clm_38a,
min(c.clm_38b) as clm_38b,
min(c.clm_12a) as clm_12a,
min(c.clm_12b) as clm_12b,
min(c.clm_60a) as clm_60a,
min(m.mem_altid) as AltId,
min(c.clm_5) as clm_5,
min(c.clm_1a1) as clm_1a1 ,
min(c.clm_1a2) as clm_1a2,
min(c.clm_deg) as clm_deg,
min(pro_spec1) as specialty,
min(v3.val_desc) as ProDesc1,
min(pro_xtyp) as protype,
min(c.clm_1a) as clm_1a,
min(c.clm_batch) as clm_batch,
min(c.clm_id1) as clm_id1,
min(s.clms_line) as clms_line,
min(s.clms_from) as clms_from,
min(s.clms_thru) as clms_thru,
impact.dbo.u_poscalc3(min(clm_form),min(clm_4),min(clms_rev),min(clm_spc),min(clms_pos),min(clms_ben),min(clm_67)) as clm_POS,
min(s.clms_rev) as clms_rev,
min(v6.val_desc) as procdesc2,
min(s.clms_proc) as clms_proc,
min(v4.val_desc) as procdesc,
min(s.clms_mod1) as clms_mod1,   
min(v5.val_desc) as moddesc,
min(c.clm_67) as Diagnosis,
min(v1.val_desc) as Dcode,
min(v1.val_opt) as class,
min(v2.val_desc) as Dclass,
min(c.clm_68) as clm_68,
min(c.clm_69) as clm_69,
min(s.clms_chg) as clms_chg,
min(s.clms_sku) as clms_sku,
min(s.clms_allow) as clms_allow,
min(s.clms_copa) as clms_copa,
min(s.clms_dedu) as clms_dedu,
min(s.clms_coin) as clms_coin,         
min(s.clms_non) as clms_non,
min(s.clms_cobp) as clms_cobp,
min(s.clms_wh) as clms_wh,   
min(s.clms_payp) as clms_payp,
min(s.clms_paye) as clms_paye,
min(c.clm_inet) as clm_inet,
min(s.clms_ben) as clms_ben,
min(c.clm_form) as clm_form,
Min(c.clm_stades) as clm_stades,
min(c.clm_edid) as clm_edid,
Min(c.clm_chkno) as clm_chkno,
min(c.clm_dout) as clm_dout,
min(c.clm_chgdt) as clm_chgdt,
min(c.clm_ips) as clm_ips,
min(c.clm_den) as Den,
min(clm_pclm) as TXENnum,
min(d.clmd_cm01) as prindiag,
min(icd10.DIAG_CODE) as diagcode
into #test1
from impact.dbo.tbl_clm c
left join impact.dbo.tbl_mem m on m.mem_id1 = c.clm_12
left join impactwork.dbo.icd_diag_codes icd9 on icd9.diag_code =                 left(c.clm_67, 3) and icd9.code_type = 'ICD9_DIAG'
left join impact.dbo.tbl_clmd d on clmd_id1 = c.clm_id1
left join impactwork.dbo.icd_diag_codes icd10 on icd10.diag_code =      left(d.clmd_cm01, 3) and icd10.code_type = 'ICD10_DIAG'
left join impact.dbo.tbl_meme e on e.meme_id1 = c.clm_12
left join impact.dbo.tbl_clms s on s.clms_id = c.clm_id1
left join impact.dbo.tbl_valid diag on diag.val_code = clm_67 and         diag.val_type = '503'
left join tbl_valid v1 on v1.val_type = '57a'and v1.val_code = c.clm_67       --primary diagnosis
left join tbl_valid v2 on v2.val_type = '57b' and v2.val_code = v1.val_opt    --      The val_opt of the code relates to the class
left join impact.dbo.tbl_pro p  on c.clm_1 = p.pro_id1
left join tbl_valid v3 on v3.val_type = '302'and v3.val_code = pro_spec1
left join tbl_valid v4 on v4.val_type = '501'  and v4.val_code = clms_proc 
left join tbl_valid v5 on v5.val_type = '502'  and v5.val_code = clms_mod1
left join tbl_valid v6 on v6.val_type = '506'  and v6.val_code = clms_rev
where c.clm_stades = 'paid' 
and c.clm_stades != 'dupl'
and clm_form != 'B'
and clms_Actn != 'Change'           
and c.clm_prod  !='den'
and c.clm_dout between '01/01/2015' and '05/31/2015' 
and c.clm_prod <> 'flx'
and clm_cc2 in ('52037')
group by c.clm_id1,s.clms_line,c.clm_cc2
order by c.clm_Id1,s.clms_line,c.clm_cc2

select *

from #test1

答案 1 :(得分:-1)

Table impact.dbo.tbl_clmd is aliased as d (longer aliases FTW, helps readability). If you're seeing the error

'The multi-part identifier "d.clmd_cm01" could not be bound.

Then I'd be almost certain that table impact.dbo.tbl_clmd doesn't contain a column clmd_cm01.

EDIT - Kamran Farzami's spotted what I missed, that the table aliased in d is only joined in after it's been used as a join criteria. In any RDMBS I've used they have to be declared in order - you can't depend on what hasn't already been referenced in the query.