Comparing values in two rows and getting ORA-00918: column ambiguously defined - sql

I am trying to compare values from one column in two different rows using CASE statement. Inner SELECT statements are identical and working OK giving me fields that I need. Then I inner joined them on one of the key fields (KYCID). That's where I get "column ambiguously defined" error. I tried to remove duplicate rows by using DISTINCT - still the same error.
SELECT DISTINCT e.KYCID,
CASE WHEN e.HRAC_FLAG <> f.HRAC_FLAG THEN 'FALSE' ELSE 'TRUE' END AS FLAG_COMPARISON
FROM
(SELECT gt.task_id, gt.work_item_status, gt.work_item_type, gt.component_id, xref.HRAC_FLAG,
xref.case_nbr, xref.task_id, d.kycid, d.core_component_state
FROM kyc_gbl_main.global_task gt
inner join KYC_RGN_NAM_MAIN.CASE_HRAC_XREF xref on gt.component_id = xref.component_id
inner join kyc_rgn_nam_main.account a on xref.accountid = a.accountid
inner join kyc_rgn_nam_main.country_appx_account_xref b on a.accountid = b.accountid
inner join kyc_rgn_nam_main.country_appx c on c.cntry_appx_id = b.cntry_appx_id and
c.country_appx_state = b.country_appx_state
inner join kyc_rgn_nam_main.kyc_main d on d.kycid = c.kycid
where gt.work_item_type = 'HRAC_OVERLAY'
and gt.work_item_status = 'Completed') e
INNER JOIN
(select gt.task_id, gt.work_item_status, gt.work_item_type, gt.component_id, xref.HRAC_FLAG,
xref.case_nbr, xref.task_id, d.kycid, d.core_component_state
from kyc_gbl_main.global_task gt
inner join KYC_RGN_NAM_MAIN.CASE_HRAC_XREF xref on gt.component_id = xref.component_id
inner join kyc_rgn_nam_main.account a on xref.accountid = a.accountid
inner join kyc_rgn_nam_main.country_appx_account_xref b on a.accountid = b.accountid
inner join kyc_rgn_nam_main.country_appx c on c.cntry_appx_id = b.cntry_appx_id and
c.country_appx_state = b.country_appx_state
inner join kyc_rgn_nam_main.kyc_main d on d.kycid = c.kycid
where gt.work_item_type = 'HRAC_OVERLAY'
and gt.work_item_status = 'Completed') f
ON e.KYCID = f.KYCID
WHERE e.core_component_state ='ACTIVE'
AND f.core_component_state = 'IN_PROGRESS';

As determined with process of elimination, you reference the same named column in a SELECT query. To avoid this name collision, consider aliasing those particular columns.
Additionally, to avoid repetition, consider using a CTE via WITH and avoid using table aliases like (a, b, c) or (t1, t2, t3). Finally, move WHERE conditions to ON to filter before combining all data sources.
WITH sub AS (
SELECT gt.task_id AS gt_task_id -- RENAMED TO AVOID COLLISION
, gt.work_item_status
, gt.work_item_type
, gt.component_id
, xref.HRAC_FLAG
, xref.case_nbr
, xref.task_id AS x_ref_task_id -- RENAMED TO AVOID COLLISION
, k.kycid
, k.core_component_state
FROM kyc_gbl_main.global_task gt
INNER JOIN KYC_RGN_NAM_MAIN.CASE_HRAC_XREF xref
ON gt.component_id = xref.component_id
AND gt.work_item_type = 'HRAC_OVERLAY' -- MOVED FROM WHERE
AND gt.work_item_status = 'Completed' -- MOVED FROM WHERE
INNER JOIN kyc_rgn_nam_main.account acc
ON xref.accountid = acc.accountid
INNER JOIN kyc_rgn_nam_main.country_appx_account_xref cax
ON acc.accountid = cax.accountid
INNER JOIN kyc_rgn_nam_main.country_appx ca
ON ca.cntry_appx_id = cax.cntry_appx_id
AND ca.country_appx_state = cax.country_appx_state
INNER JOIN kyc_rgn_nam_main.kyc_main k
ON k.kycid = ca.kycid
)
SELECT DISTINCT
e.KYCID
, CASE
WHEN e.HRAC_FLAG <> f.HRAC_FLAG
THEN 'FALSE'
ELSE 'TRUE'
END AS FLAG_COMPARISON
FROM sub e
INNER JOIN sub f
ON e.KYCID = f.KYCID
AND e.core_component_state = 'ACTIVE' -- MOVED FROM WHERE
AND f.core_component_state = 'IN_PROGRESS' -- MOVED FROM WHERE

Related

Multiple Join not working on two string attributes

I have the following issue:
I have several tables in my Database, in order to check for a specific criteria I have to join several tables, where I use the following statement:
SELECT *
FROM (SELECT * FROM (((((((((SELECT * FROM table1 WHERE tab1_id = 1) AS A
INNER JOIN (SELECT * FROM table2 WHERE tab2_variable IS NOT NULL) AS B
ON A.variable = B.variable)
INNER JOIN (SELECT table3.*, IIF(year='XXXX', 0, 1) AS flag FROM table3) AS C
ON A.h_name = C.h_name)
INNER JOIN (SELECT * FROM table4 WHERE s_flag = 0) AS D
ON C.v_type = D.v_type)
INNER JOIN table5
ON C.ng_type = table5.ng_type)
INNER JOIN table6
ON C.part = table6.part)
INNER JOIN table7
ON C.ifg = table7.ifg)
INNER JOIN table8
ON C.v_type = table8.v_type AND C.ng_typ = table8.ng_typ AND C.ntr_flag = table8.ntr_flag)
INNER JOIN table9
ON table8.ifg_sii_id = table9.ifg_sii_id)) AS F
LEFT JOIN table10
ON F.risk = table10.risk AND C.v_type = table10.v_type
AND F.series = table10.series
This statement fails. But when I remove one the following two join-conditions in the last Left JOIN, it works as intended:
F.risk = table10.risk and/or C.v_type = table10.v_type
They are both of type CHAR, whereas series is type TINYINT, I guess it has something to do with joining on multiple conditions with strings, but I'm not able to find a workaround, any ideas?
according to your current SQL, Table C is not visible as it's a sub query within F. instead of C.v_type you need to use F.c_v_type and the c_v_type field must come from C table like. select v_type as c_v_type from table3... as C
In the last part of your Query You could try to actually select the table and include the Where Clause Like so:
LEFT JOIN (Select * from table10 Where C.v_type = v_type AND F.series = series) as xx
ON F.risk = xx.risk
Hope this helps

Select statement where joins causing records to be excluded

I have the below query with multiple joins.The last 3 joins are required to get the g.fin_id value. This works fine (see results) BUT because some records in the ACCUM_ISS_CHAR_HIST table have e.char9_nme values of NULL, it excludes the records in the results altogether. So it seems as when the e.char9_nme value has a record then it will produce a result, but as soon as it has a Null value then it is excluded. I would still like to see the records even though the g.fin_id for those will then be blank because they have a e.char9_nme value of Null. How can I change the query to accomplish this?
select
a.acct_id,
c.fld3_txt,
b.issue_loc1_cde,
b.instr_id,
a.fld1_nme,
b.issue_cls2_nme,
g.fin_id,
e.char9_nme
from position_dg as a
inner join
infoportal..issue_dg as b on b.INSTR_ID = a.INSTR_ID
inner join
InfoPortal..IVW_ACCT as c on a.acct_id = c.acct_id
inner join
InfoPortal..DW_AcctCharDG as d on a.acct_id = d.acctid
inner join
ACCUM_ISS_CHAR_HIST as e on a.instr_id = e.instr_id
inner join
MD_FINANCIAL_ENTITY as f on e.char9_nme = f.fin_enty_name
inner join md_FINANCIAL_ENTITY_ALTERNATE_IDENTIFIER as g on
f.fin_enty_id = g.fin_enty_id
and b.MAT_EXP_DTE > getdate()
and b.issue_cls1_nme = 'Derivatives'
and a.as_of_tms >= getdate()-1
and b.iss_typ in ('FFX','IRS','EQF')
and d.AcctChrSetId = 'DerivativeRpt'
and d.EndTms IS NULL
and a.acct_id = 'FOGEMBLCR'
and g.id_ctxt_typ = 'LEGAL_ENTITY_IDENTIFIER'
and e.as_of_dte = (
select MAX (as_of_dte)-1
from accum_iss_char_hist
)
I expect the results to show fin_id records for some ond blank fin_id records for some, but at the moment only the ones with a fin_id record is hown and the rest is excluded from the results.
You are looking for a left join.
Join all those tables (last 3 as you said) as left join. For better clarity I have moved conditions of every tables in their ON clause and for base table a made a where clause.
select
a.acct_id,
c.fld3_txt,
b.issue_loc1_cde,
b.instr_id,
a.fld1_nme,
b.issue_cls2_nme,
g.fin_id,
e.char9_nme
from position_dg as a
inner join
infoportal..issue_dg as b on b.INSTR_ID = a.INSTR_ID
and b.MAT_EXP_DTE > getdate()
and b.issue_cls1_nme = 'Derivatives'
and b.iss_typ in ('FFX','IRS','EQF')
inner join
InfoPortal..IVW_ACCT as c on a.acct_id = c.acct_id
inner join
InfoPortal..DW_AcctCharDG as d on a.acct_id = d.acctid
and d.AcctChrSetId = 'DerivativeRpt'
and d.EndTms IS NULL
left join
ACCUM_ISS_CHAR_HIST as e on a.instr_id = e.instr_id
and e.as_of_dte = (
select MAX (as_of_dte)-1
from accum_iss_char_hist
)
left join
MD_FINANCIAL_ENTITY as f on e.char9_nme = f.fin_enty_name
left join
md_FINANCIAL_ENTITY_ALTERNATE_IDENTIFIER as g on f.fin_enty_id = g.fin_enty_id
and g.id_ctxt_typ = 'LEGAL_ENTITY_IDENTIFIER'
Where a.as_of_tms >= getdate()-1
and a.acct_id = 'FOGEMBLCR'

Multi-part identifier could not be bound?

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.
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
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.

Getting error: "Column ambiguously defined "

I am running the following query and getting column ambiguously defined :
SELECT
S.SUB_ID
,M.FLEETID
,M.TGID
,M.TGNO
,R.TGTYPE
,R.MODEID
,COUNT(1)
FROM
INF_SUBSCRIBER_ALL S
INNER JOIN
INF_TALKGROUP_MEMBER M ON S.SUB_ID = M.SUBID
INNER JOIN
INF_TALKGROUP_MODE_RELATION R ON M.TGID = R.TGID
INNER JOIN
INF_TALKGROUP_MODE_RELATION R ON M.FLEETID = R.FLEETID
WHERE
S.SUB_STATE = 'B01'
AND M.STATUS = 'M01'
GROUP BY
S.SUB_ID, M.FLEETID, M.TGID, M.TGNO, R.TGTYPE, R.MODEID;
You have used aliase-R twice, that is why the error,
You dont need to include a table twice into JOINs for adding another condition(M.FLEETID = R.FLEETID),
You can give the corresponding condition in the first occurce itselt using AND operator.
SELECT
S.SUB_ID
,M.FLEETID
,M.TGID
,M.TGNO
,R.TGTYPE
,R.MODEID
,COUNT(1)
FROM
INF_SUBSCRIBER_ALL S
INNER JOIN
INF_TALKGROUP_MEMBER M ON S.SUB_ID = M.SUBID
INNER JOIN
INF_TALKGROUP_MODE_RELATION R ON M.TGID = R.TGID AND M.FLEETID = R.FLEETID
WHERE
S.SUB_STATE = 'B01'
AND M.STATUS = 'M01'
GROUP BY
S.SUB_ID, M.FLEETID, M.TGID, M.TGNO, R.TGTYPE, R.MODEID;
Hope this helps.

SQL Server update with joins

I am trying to update a date in a table, based off of a MAX(date) in another table. To get the correct data to link up, I have to do 2 inner joins and 2 left outer joins.
I can select the correct data, it returns a Guid (PersonId) and the Date.
I have to use this information to update my original table. I am having trouble getting this to work, I still getting syntax errors.
update tblqualityassignments as assign
inner join tblrequirementteams as team on assign.guidmemberid = team.guidmemberid
set assign.dtmQAPCLed = dtmTaken
from
(
select reg.guidpersonid, max(certs.dtmTaken) as dtmTaken from tblqualityassignments as assign
inner join tblrequirementteams as team on assign.guidmemberid = team.guidmemberid
inner join tblregisteredusercerts as reg on team.guidpersonid = reg.guidpersonid
left outer join tblcerttaken as certs on certs.guidcertid = reg.guidcertid
left outer join tblCodesCertType as types on types.intcerttypeid = certs.intcerttypeid
where types.intcerttypeid = 1
and assign.guidmemberid = team.guidmemberid
group by reg.guidpersonid as data
)
where data.guidpersonid = team.guidpersonid
Assuming you are using SQL Server for this, then this should work:
UPDATE A
SET A.dtmQAPCLed = dtmTaken
FROM tblqualityassignments AS A
INNER JOIN tblrequirementteams as T
ON A.guidmemberid = T.guidmemberid
INNER JOIN (select reg.guidpersonid, max(certs.dtmTaken) as dtmTaken
from tblqualityassignments as assign
inner join tblrequirementteams as team
on assign.guidmemberid = team.guidmemberid
inner join tblregisteredusercerts as reg
on team.guidpersonid = reg.guidpersonid
left outer join tblcerttaken as certs
on certs.guidcertid = reg.guidcertid
left outer join tblCodesCertType as [types]
on [types].intcerttypeid = certs.intcerttypeid
where [types].intcerttypeid = 1
and assign.guidmemberid = team.guidmemberid
group by reg.guidpersonid) data
ON T.guidpersonid = data.guidpersonid