Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old June 24th, 2003, 07:19 AM
Authorized User
 
Join Date: Jun 2003
Location: Mount Gilead, Ohio, USA.
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Default Joins

Hi folks,
  I need some help in ocmstructing a join.
I have three tables. The structure is:

Tables (columns listed underneath table name):
1.SoldProducts
     Area
     ProductId
     SalesmandId

2.Salesman
     SalesmanId
     Name
     BranchPk

3.Branch
     BranchPk
     BranchId
     BranchName



What I need to do is select from the table SoldProducts where the area equals some value and where the BranchId equals some value.
This what I have so far:

   Select SoldProducts.Area,SoldProducts.ProductId,
          Salesman.Name,Branch.BranchId,Branch.Name
            From SoldProducts
                inner join Salesman on Salesman.SalesmanId = SoldProducts.SalesmanId
              join Salesman on Branch.BranchPk = Salesman.BranchPk
               where SoldProducts.Area = some value
               And Branch.BranchPk = some value


     heavens knows this isn't right. What do i need tod o and understand to be able to construct this properly. I can not change the strucure of the tables. Thank you so very much
marthaj
Reply With Quote
  #2 (permalink)  
Old June 24th, 2003, 07:30 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: Hudson, MA, USA.
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

You're close. Try:
Code:
SELECT SoldProducts.Area,SoldProducts.ProductId,Salesman.Name,Branch.BranchId,Branch.Name
   FROM SoldProducts
    INNER JOIN Salesman ON SoldProducts.SalesmanID=Salesman.SalesmanID
    INNER JOIN Branch ON Salesman.BranchPk=Branch.BranchPk
WHERE SoldProducts.Area = @areavalue
  AND Branch.BranchPk = @Branchvalue;

Jeff Mason
Custom Apps, Inc.
www.custom-apps.com
Reply With Quote
  #3 (permalink)  
Old June 24th, 2003, 07:31 AM
Authorized User
 
Join Date: Jun 2003
Location: Edinburgh, , United Kingdom.
Posts: 87
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi

I hope this helps....

Select SoldProducts.Area,SoldProducts.ProductId,
Salesman.Name,Branch.BranchId,Branch.Name
From SoldProducts sp inner join
     Salesman s on sp.SalesmanId = s.SalesmanId inner join
     Branch b on b.BranchPk = s.BranchPk
where SoldProducts.Area = some value
And Branch.BranchPk = some value

Cheers

Nickie
Reply With Quote
  #4 (permalink)  
Old June 25th, 2003, 09:33 AM
Authorized User
 
Join Date: Jun 2003
Location: Mount Gilead, Ohio, USA.
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you all for replying.
Would the following be correct too ??


Select a.Area,a.ProductId,
b.Name,b.BranchId,c.Name
From SoldProducts
inner join Salesman as a on b.SalesmanId = a.SalesmanId
inner join Branch as c on c.BranchPk = b.BranchPk
where a.Area = some value
And c.BranchPk = some value
Reply With Quote
  #5 (permalink)  
Old June 25th, 2003, 10:30 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: Hudson, MA, USA.
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

Quote:
quote:Would the following be correct too ??

Select a.Area,a.ProductId,
b.Name,b.BranchId,c.Name
From SoldProducts
inner join Salesman as a on b.SalesmanId = a.SalesmanId
inner join Branch as c on c.BranchPk = b.BranchPk
where a.Area = some value
And c.BranchPk = some value
No.

Your aliases are confused. An alias is simply a name you give to a table mentioned in the FROM clause. If you give a table an alias in the FROM clause, you must then refer to columns in that table using that table alias as a prefix to the column name everywhere else in the query. You do not need to prefix the table name or alias to a column if the column name can be unambiguously determined from all the tables in the FROM clause.

It is helpful to use mnemonic alias names for your tables so it is easier to understand which table a column expression is referring to. Using an arbitrary 'a', 'b', and 'c' makes your query very hard to read and understand.

Note that the 'as' keyword is optional when defining an alias. I generally do not use it, as I think it tends to clutter up the query.

I would write your query using aliases as:
Code:
SELECT p.Area, p.ProductId, s.Name, b.BranchId, b.BranchName
   FROM SoldProducts p 
      INNER JOIN Salesman s ON p.SalesmanId = s.SalesmanId
      INNER JOIN Branch b ON s.BranchPk = b.BranchPk
   WHERE p.Area = some value
     AND b.BranchPk = some value



Jeff Mason
Custom Apps, Inc.
www.custom-apps.com
Reply With Quote
  #6 (permalink)  
Old June 26th, 2003, 07:41 AM
Authorized User
 
Join Date: Jun 2003
Location: Mount Gilead, Ohio, USA.
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you kindly for your response and for the advise and tips.
I see what you are saying about confusion and the alias for table name.It makes better sense.
The one thing that also consfuses me, is which qualifier is used first on the equal sign. Such as

INNER JOIN Branch b ON s.BranchPk = b.BranchPk


    What rule can I hold in my mind to prevent errors ?? I don't know if I am reading too much or thinking too little and confusing myself.
Again, I thank you for the time you have given.
Reply With Quote
  #7 (permalink)  
Old June 26th, 2003, 08:17 AM
Authorized User
 
Join Date: Jun 2003
Location: , , .
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The order in which you specify the ON portion of the join clause does not matter, i.e. "INNER JOIN Branch b ON s.BranchPk = b. BranchPk" is the same as "INNER JOIN Branch b ON b.BranchPk = s.BranchPk (at least for MSSQL, I don't know about other DBMS). Also, for an inner join or full outer join the order in which you specify the tables doesn't matter. Of course if you use a left or right join, the order of the tables would matter, but the ON portion would not.

-Mike
Reply With Quote
  #8 (permalink)  
Old June 26th, 2003, 09:02 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: Hudson, MA, USA.
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

As the previous poster has said, it doesn't matter; 'ON a=b' is exactly equivalent to 'ON b=a'. I like to put the order of testing in the same order as the tables mentioned in the FROM clause, e.g.:
Code:
    ...
    FROM Table1 INNER JOIN Table2
        ON Table1.col=Table2.col
    ...
...but this purely a matter of taste, I think. The optimizer will freely rearrange the order of evaluation of the expressions to suit its query plan anyway, so the order is only for your benefit.

Jeff Mason
Custom Apps, Inc.
www.custom-apps.com
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What are joins? Bhalchandra SQL Server 2000 2 July 2nd, 2007 11:29 PM
Problems with joins brettdalldorf SQL Server 2000 4 December 22nd, 2005 02:50 AM
Joins nalla Oracle 0 December 14th, 2005 05:54 AM
joins on more than 2 tables badgolfer Access 2 February 22nd, 2005 07:13 PM
Joins r_ganesh76 SQL Server 2000 2 February 10th, 2005 12:21 AM



All times are GMT -4. The time now is 10:07 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.