sql server - Sql inner joins query -


i working on query , don't know how use inner join on 3 different tables 1 table linked other 2 tables only.

individual queries work fine:

select sl_letter_batch,cctvpcn_run_date,cctvpcn_post_date  cctvpcn_batches,statutory_letter  sl_system_ref = 1095278 , sl_letter_batch = cctvpcn_batch  order cctvpcn_run_date  select sl_letter_batch,nto_run_date,nto_post_date  nto_batches,statutory_letter  sl_system_ref = 1095278 , sl_letter_batch = nto_batch  order nto_run_date 

now if want inner join same tables :

select sl_letter_batch,cctvpcn_run_date,cctvpcn_post_date  cctvpcn_batches,statutory_letter    inner join nto_batches,statutory_letter , sl_letter_batch = nto_batch  , sl_letter_batch = cctvpcn_batch    sl_system_ref = 1095278  order nto_run_date 

i know syntax error trying different. because sl_letter_batch has different values in 2 tables. result null.

sl_letter_batch cctvpcn_run_date    cctvpcn_post_date 21326   2014-10-07 12:45:06.000 2014-10-07 00:00:00.000 21571   2014-11-25 14:13:55.000 2014-11-25 00:00:00.000  sl_letter_batch nto_run_date    nto_post_date 21502   2014-11-13 09:06:24.000 2014-11-13 00:00:00.000 21785   2015-01-05 14:30:42.000 2015-01-05 00:00:00.000 

is there anyway write query both table results join.

here mixed 2 sql styles - old , new. in old - 2 tables joined using ","

.. cctvpcn_batches, statutory_letter ... 

in new - used syntax "join tablename on id1 = id2 ..." - here 1 table per "join" , each "join" word should have matching "on".

select ... cctvpcn_batches inner join statutory_letter       on    sl_letter_batch = cctvpcn_batch inner join nto_batches       on    sl_letter_batch = nto_batch ... 

you still can combine styles.

select ... cctvpcn_batches, statutory_letter inner join nto_batches         on sl_letter_batch = nto_batch sl_letter_batch = cctvpcn_batch       ... 

pair "join ... on ..." can olso used brackets:

select ... cctvpcn_batches inner join nto_batches     inner join statutory_letter     on    sl_letter_batch = cctvpcn_batch on sl_letter_batch = nto_batch ... 

here join "nto_batches" before doing join "statutory_letter" "cctvpcn_batches". that's why there 2 sequential "on" - first "statutory_letter", second - "nto_batches"

if need outer join (including mismatches) then:

select ... statutory_letter  left outer join cctvpcn_batches       on    sl_letter_batch = cctvpcn_batch left outer join nto_batches       on    sl_letter_batch = nto_batch ... 

Comments

Popular posts from this blog

cakephp - simple blog with croogo -

How to group boxplot outliers in gnuplot -

bash - Performing variable substitution in a string -