I have these five tables and have an expected outcome for JOIN them.
Example
Table JobShipment
Table Jobheader
Table Branch
Table Company
Table Notetext
My Expected outcome
The outcome is not what I expected.
My query and result
SELECT JS.JS_JobNumber as 'JobNumber', gbb.GB_Code AS 'Branch' , gb.GB_Code as 'Company' ,jh.jh_Dept as 'Dept', ST.ST_NoteText AS 'Note Text'
FROM notetext st (NOLOCK)
LEFT JOIN Company gc (NOLOCK) on st.st_gc_relatedCompany = gc.gc_pk
LEFT JOIN jobshipment js (NOLOCK) ON st.ST_ParentID = js.JS_PK
LEFT JOIN jobheader jh (NOLOCK) on jh.jh_parentID = js.js_pk
left JOIN Branch gbb (NOLOCK) on jh.jh_ge = gbb.GB_PK
left JOIN Branch gb (NOLOCK) ON GB.gb_company = gc.gc_pk AND gbb.gb_pk = gb.gb_pk
where JS.JS_JobNumber = 'S0154'
Why does notetext appear in branch 'CLE'?