Solved

sql query that filters on a related table

Posted on 2010-11-16
8
230 Views
Last Modified: 2012-05-10
Hi

I have two tables that I need to make a report from, but I can't figure out the SQL.

Basically I want all rows from table SAPModules, and then add some fields from SAPSkills.
SAPModules lists all available choices, while SAPSkills lists the already chosen skills for each person.

The result should be a list of all the SAP modules available plus added data in those rows that exists in SAPSkills.

My problem starts when I want to filter those lists by PersonID in SAPSkills. How do I cange the SQL so that I can print out a list of all SAPModules, but only include the extra data from SAPSkills relevant for each individual person?

SELECT     SAPModules.Code + N' ' + SAPModules.Name AS SAPModule, SAPSkills.StartDate, SAPSkills.EndDate, SAPSkills.PersonID
FROM         SAPSkills RIGHT OUTER JOIN
                      SAPModules ON SAPSkills.SAPModuleID = SAPModules.SAPModuleID
WHERE     (SAPSkills.PersonID = @PersonID)

TIA
Dennis
0
Comment
Question by:sgude0
  • 4
  • 4
8 Comments
 
LVL 57

Expert Comment

by:Raja Jegan R
ID: 34145716
This should do: (Just a rewrite using LEFT JOIN for easier reading)

SELECT SAPModules.Code + N' ' + SAPModules.Name AS SAPModule, SAPSkills.StartDate, SAPSkills.EndDate, SAPSkills.PersonID
FROM SAPModules
LEFT JOIN SAPSkills ON SAPSkills.SAPModuleID = SAPModules.SAPModuleID
WHERE (SAPSkills.PersonID = @PersonID)
0
 
LVL 57

Expert Comment

by:Raja Jegan R
ID: 34145725
>> How do I cange the SQL so that I can print out a list of all SAPModules, but only include the extra data from SAPSkills relevant for each individual person?

Not sure on what you meant by Print out, kindly clarify
Also post some sample records to explain what you are trying to achieve.
0
 

Author Comment

by:sgude0
ID: 34145847
This still doesn't work, as I now only get records from SAPSkills with fields from SAPModules, I want it the other way around.

The fields in the resulting list should be like this:

1: SAPModules.Name (every line)
2: SAPSkills.StartDate (only if selected PersonID has one, else NULL)
2: SAPSkills.EndDate (only if selected PersonID has one, else NULL)

Re
Dennis
0
 
LVL 57

Expert Comment

by:Raja Jegan R
ID: 34145908
>> WHERE (SAPSkills.PersonID = @PersonID)

Since you have this WHERE condition passed, your query would bring records only if you have records in SAPSkills table since PersonID is present in SAPSkills table alone.
Else it won't bring any records.
Can you kindly clarify your exact requirements..
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:sgude0
ID: 34146005
OK, I think I understand that problem. Let's do it step by step ;-)

The following statement gets the list I want:

SELECT     Code + N' ' + Name AS SAPModule
FROM         SAPModules

I will give each employee this list (to update their CV's). However, since most employees have already registered skills in the past, this information should be added to their list so they dont have to re-do the registration, just update the new stuff they've learned since the last registration.

I've included an Excel sheet with the basic data.

Re
Dennis


SkillsUpdate.xls
0
 
LVL 57

Accepted Solution

by:
Raja Jegan R earned 500 total points
ID: 34146587
Have gone through the data in Excel sheet and hope this is what you require.
SELECT t1.SAPModule, SAPSkills.StartDate, SAPSkills.EndDate, SAPSkills.PersonID
FROM (
SELECT DISTINCT Code + N' ' + Name AS SAPModule, SAPModuleID, @PersonID PersonID
FROM SAPModules) t1
LEFT JOIN SAPSkills ON t1.PersonID = SAPSkills.PersonID
AND SAPSkills.SAPModuleID = t1.SAPModuleID
WHERE SAPSkills.PersonID = @PersonID

Open in new window

0
 

Assisted Solution

by:sgude0
sgude0 earned 0 total points
ID: 34146824
Nope, that still only shows the SAPModule rows that has SAPSkill rows, but I got the answer from MSDN. This does what I want:

SELECT     SAPModules.SAPModuleID, SAPModules.Name, SAPSkills.PersonID, SAPSkills.StartDate, SAPSkills.EndDate
FROM         SAPModules LEFT OUTER JOIN
                      SAPSkills ON SAPModules.SAPModuleID = SAPSkills.SAPModuleID AND SAPSkills.PersonID = @PersonID
0
 

Author Closing Comment

by:sgude0
ID: 34182417
Thanks for the help though.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now