Solved

sql query that filters on a related table

Posted on 2010-11-16
8
233 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
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
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

Backup Solution for AWS

Read about how CloudBerry Backup fully integrates your backups with Amazon S3 and Amazon Glacier to provide military-grade encryption and dramatically cut storage costs on any platform.

Question has a verified solution.

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

Suggested Solutions

Audit has been really one of the more interesting, most useful, yet difficult to maintain topics in the history of SQL Server. In earlier versions of SQL people had very few options for auditing in SQL Server. It typically meant using SQL Trace …
     When we have to pass multiple rows of data to SQL Server, the developers either have to send one row at a time or come up with other workarounds to meet requirements like using XML to pass data, which is complex and tedious to use. There is a …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

713 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