Solved

sql query that filters on a related table

Posted on 2010-11-16
8
231 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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 

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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

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

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 …
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

863 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

28 Experts available now in Live!

Get 1:1 Help Now