Solved

sql query that filters on a related table

Posted on 2010-11-16
8
235 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
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
 

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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is basically a blog post I wrote recently. I've found that SARGability is poorly understood, and since many people don't read blogs, I figured I'd post it here as an article. SARGable is an adjective in SQL that means that an item can be fou…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

622 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