简体   繁体   中英

How to join a 4th table which doesn't have common column

I have a 4 tables, detail_table, adders_table, form_table and summary_table first 3 table have a common column piece_ID but 4th table summary_table is don't have a Piece_ID column but instead it has ID column which is present only in detail table not in address and form table Now is there any way I can join all these 4 tables together?

(I have successfully join form, address and detail tables only remained is summary) Below is the query with 3 tables and I got successful result from this.

    Select archive_address.PieceID, archive_adf_detail.Job_ID, prod_code, 
seq_number,process_date, seq_queue, addline1, addline2,addline3,addline4,addline5, archive_freeform.free_form1, archive_Free_Form2
from archive_Address join archive_adf_detail
on archive_address.PieceID = archive_adf_detail.F_key
join archive_freeform
on archive_address.PieceID = archive_freeform.PieceID
Where (archive_Address.addline1 like '%Cons%' 
or archive_Address.addline2 like '%Cons%' )
and archive_Address.pieceID like '%FB2K%'
order by addline1

I am using below query to add 4th table with this

Select archive_address.PieceID, archive_adf_detail.Job_ID, prod_code, job_summary.Status_Date,
seq_number,process_date, seq_queue, addline1, addline2,addline3,addline4,addline5, archive_freeform.free_form1, archive_freeform.Free_Form2
from archive_Address join archive_adf_detail
on archive_address.PieceID = archive_adf_detail.F_key
join archive_freeform
on archive_address.PieceID = archive_freeform.PieceID
join job_summary 
on archive_adf_detail.job_ID = job_summary.job_ID
Where (archive_Address.addline1 like '%Cons%' 
or archive_Address.addline2 like '%Cons%' )
and archive_Address.pieceID like '%FB2K%'
order by addline1

after executing the above I am getting below error:

Msg 207, Level 16, State 1, Line 8
Invalid column name 'jobID'.
Msg 209, Level 16, State 1, Line 1
Ambiguous column name 'prod_code'.
Msg 209, Level 16, State 1, Line 2
Ambiguous column name 'seq_number'.
Msg 209, Level 16, State 1, Line 2
Ambiguous column name 'process_date'.
Msg 207, Level 16, State 1, Line 2
Invalid column name 'archive_Free_Form2'.

Please correct me if I am using incorrect syntax or it not possible to join 4th table like this Thank you!

Join tables based on the relationship that the query is expressing. Your joins are fine.

The error relates to the fact that the names of columns are not unique amongst the collection of tables (Ambiguous: having multiple meanings). You need to specify which table with the column name.

Select 
    archive_address.PieceID, 
    archive_Address.addline1,
    archive_Address.addline2,
    archive_Address.addline3,
    archive_Address.addline4,
    archive_Address.addline5,
    archive_adf_detail.prod_code,
    archive_adf_detail.seq_number,
    archive_adf_detail.process_date,
    archive_adf_detail.seq_queue,
    archive_freeform.Job_ID,
    archive_freeform.free_form1,
    archive_freeform.Free_Form2,
    job_summary.Status_Date
From archive_Address 
Join archive_adf_detail on archive_address.PieceID = archive_adf_detail.F_key
Join archive_freeform on archive_address.PieceID = archive_freeform.PieceID
Join job_summary on archive_freeform.job_ID = job_summary.job_ID
Where (archive_Address.addline1 like '%Cons%' or archive_Address.addline2 like '%Cons%' )
and archive_Address.pieceID like '%FB2K%'
Order by archive_Address.addline1

I'm not totally sure you've conveyed the schema, as the error messages in your question don't correspond to the second query. You need to have an understanding of what data is where, and which parts you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM