Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LEFT OUTER JOIN 3 TABLES -SQL SERVER

Tags:

sql-server

I need to join 3 tables using left outer join .Let me give you an example using 3 tables .In the below image we can see Table:[A.TableMaster] the drugDescription Column must be equal to Table:[C.Table2]-DrugDescription column [A.drugDescription=C.DrugDescription] and [C.drug=B.drug] according to the drug the Price is to be assigned from Table:[B.Table1] . and also with the b.date

in simple english the user selects a date for the particular drug and price assigned for the drug

Image of the Tables:TableMaster,Table1,Table2 enter image description here

e.g

case WHEN Drug='OCTAGAM' THEN [b.price],

but i m not able to relate with the outer join and the three table seems confusing please Help..

like image 794
RoverRoll Avatar asked Apr 19 '26 21:04

RoverRoll


1 Answers

I guess the query is simple as that:

SELECT a.*, c.Price
  FROM TableMaster AS a
  LEFT OUTER JOIN Table2 AS b ON b.DrugDescription = a.DrugDescription
  LEFT OUTER JOIN Table1 AS c ON c.Drug = b.Drug
    AND c.Date = a.Date
like image 188
Tyron78 Avatar answered Apr 21 '26 17:04

Tyron78



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!