If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > SQL Join and Sub Query problem

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-18-06, 11:17
UncleHuck UncleHuck is offline
Registered User
 
Join Date: Mar 2004
Posts: 2
SQL Join and Sub Query problem

I'm having problems writing the SQL to Join a query with a table to show all employees for a particular company.

I have the following query so far:

SELECT Country, Region, Company, FullName, EmpNo, LRTime_Stamp, Total, LRSupervisor_EmpNo, LRSupervisor_Name
FROM qSel_All_Data_2
WHERE (Country ='England') AND (Region = 'Yorkshire') AND (Company = 'MMM') AND (Type = 'Business')
GROUP BY Country, Region, Company, FullName, LRTime_Stamp, LRSupervisor_EmpNo, LRSupervisor_Name;

I then want to join this to a table called 'Employees' and show all of the records in the 'Employees' table regardless of whether they are in the 'qSel_All_Data_2' query or not.

The 'Employees' table has the following fields:

ID
Country
Region
Company
EmpNo
Fullname

And I would want to join on Country, Region, Company, Empno.

At the moment when I try and join it only returns the records that satisfy the WHERE criteria. Do I need a subquery of some kind???

Any help would be appreciated.
Reply With Quote
  #2 (permalink)  
Old 04-18-06, 12:01
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 5,171
You need an OUTER JOIN:

SELECT e.id, ..., q.xxx, ...
FROM employees e
LEFT OUTER JOIN qSel_All_Data_2 q
ON q.country = e.country AND ...
__________________
Tony Andrews
http://tinyurl.com/tonyandrews
Reply With Quote
  #3 (permalink)  
Old 04-19-06, 12:43
pootle flump pootle flump is offline
King of Understatement
 
Join Date: Feb 2004
Location: One Flump in One Place
Posts: 14,905
Also - your group by clause appears a little superfluous
__________________
Testimonial:
Quote:
pootle flump
ur codings are working excelent.
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 On
Pingbacks are On
Refbacks are On