May 29, 2014 at 11:45 am
I know some SQL scripting but this one I've been trying to figure out for awhile. I'm using a SQL script from Report Builder to grab data from SQL. Here is an example of what I have and what I'm trying to get.
Table ProjTime
Group Project Month Hours
ABC Project X June 5
ABC Project Y June 8
ABC Project Y July 8
Table Projects
Group Projects
ABC Project X
ABC Project Y
ABC Project Z
I want to get a listing of all the projects from the Table Projects along with Month equaling June and the hours. If the Project doesn't exist in ProjTime but does in Projects then it would put 0 for hours. Here is what I want the final script to grab (could use a view if I needed to).
This is what I need for results:
Group Project Month Hours
ABC Project X June 5
ABC Project Y June 8
ABC Project Z June 0 (or null)
So basically its everything that meets the qualifications (All Projects under ABC in table Projects) and Month of June in ProjTime. Also needs to throw in month and hours if that project didn't have time put in it in June.
May 29, 2014 at 11:54 am
You need to use a LEFT JOIN between Projects and ProjTime.
If you put the month condition in the WHERE clause, it will become an INNER JOIN, so change it to the ON clause and you should get the desired output.
May 29, 2014 at 12:23 pm
Thanks for the reply Luis, I'm getting closer. Currently I have:
SELECT Distinct(Projects)
FROM Projects
LEFT JOIN ProjTime
ON Projects.Group=ProjTime.Group;
This shows all the Projects. How would I change the On Clause to grab all the Projects within Group ABC in the projects table along with the corresponding Group (ABC also) in ProjTime in the month of June?
May 29, 2014 at 12:54 pm
You can add more conditions to your JOIN using AND. I did part of the job here. You need to work on the rest.
SELECT Projects, Hours
FROM Projects
LEFT JOIN ProjTime
ON Projects.Group=ProjTime.Group
AND Projects.Project=ProjTime.Project;
May 29, 2014 at 2:05 pm
Got it Luis, I appreciate your help.
May 29, 2014 at 2:16 pm
You're welcome.
I hope that you could understand it and learn something new. 😉
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply