SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


A Refresher on Joins


A Refresher on Joins

Author
Message
Anthony Scott
Anthony Scott
SSC-Enthusiastic
SSC-Enthusiastic (106 reputation)SSC-Enthusiastic (106 reputation)SSC-Enthusiastic (106 reputation)SSC-Enthusiastic (106 reputation)SSC-Enthusiastic (106 reputation)SSC-Enthusiastic (106 reputation)SSC-Enthusiastic (106 reputation)SSC-Enthusiastic (106 reputation)

Group: General Forum Members
Points: 106 Visits: 93

Good article, especially for beginners, but useful as a simple refresher.

We discuss joins quite often in optomizing code and it's amazing how many developers forget the basic rules.

Waiting for the next article on joins though ...



________________________________________________


We passed upon the stair - and I was that man who sold the world
Arun T Jayapal
Arun T Jayapal
SSC-Enthusiastic
SSC-Enthusiastic (109 reputation)SSC-Enthusiastic (109 reputation)SSC-Enthusiastic (109 reputation)SSC-Enthusiastic (109 reputation)SSC-Enthusiastic (109 reputation)SSC-Enthusiastic (109 reputation)SSC-Enthusiastic (109 reputation)SSC-Enthusiastic (109 reputation)

Group: General Forum Members
Points: 109 Visits: 50
This was indeed a good refresher on joins...

What if you had to extract data from table_1 which does not exist in table_2 but there are two or more conditions...(By conditions I mean what comes after the 'on' keyword).
Ninja's_RGR'us
Ninja's_RGR'us
SSC Guru
SSC Guru (69K reputation)SSC Guru (69K reputation)SSC Guru (69K reputation)SSC Guru (69K reputation)SSC Guru (69K reputation)SSC Guru (69K reputation)SSC Guru (69K reputation)SSC Guru (69K reputation)

Group: General Forum Members
Points: 69537 Visits: 9671
You just add them to the where clause (if it's on the left table). If it's on the joined table then you have to make it part of the join clause...
aaron-403220
aaron-403220
SSC Journeyman
SSC Journeyman (77 reputation)SSC Journeyman (77 reputation)SSC Journeyman (77 reputation)SSC Journeyman (77 reputation)SSC Journeyman (77 reputation)SSC Journeyman (77 reputation)SSC Journeyman (77 reputation)SSC Journeyman (77 reputation)

Group: General Forum Members
Points: 77 Visits: 74
If this article is supposed to be for newbies, I think more narrative is needed for the explanation "Observe the query and output carefully...and then see if you can get the following output, which you will agree makes more sense."

Sorry, but that provides no explanation about why it would make more sense. You should point out the differences, how to achieve the output and the reason it makes more sense.
TC-416047
TC-416047
SSC Veteran
SSC Veteran (278 reputation)SSC Veteran (278 reputation)SSC Veteran (278 reputation)SSC Veteran (278 reputation)SSC Veteran (278 reputation)SSC Veteran (278 reputation)SSC Veteran (278 reputation)SSC Veteran (278 reputation)

Group: General Forum Members
Points: 278 Visits: 213
Well here is a real world scenario

from
receiver_line rl
left outer join receiver r
on (r.id=rl.receiver_id)
left outer join receiver_line_del rld
on (rl.receiver_id = rld.receiver_id and rld.receiver_line_no=rl.line_no)
left outer join purc_order_line pol
on (rl.purc_order_id = pol.purc_order_id and rl.purc_order_line_no = pol.line_no)
left outer join purchase_order po
on (po.id=rl.purc_order_id)
left outer join purc_line_del pld
on (pol.purc_order_id = pld.purc_order_id and pld.purc_order_line_no = pol.line_no)
left outer join vendor v
on (po.vendor_id=v.id)



My problem is that i get dupicate records/rows when there is a pld record/row and I do not know hwo to avoid this.

The table relationships are receivers to purchase orders
There a header(1)/line(many)/line delivery(many) relationship hend the r,rl,rld and po,pol,pld

How do I avoid the duplicate records/rows when pld exist?
Each line could have many deliveries scheduled.

tia,
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search