Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Unable to figure out SQL Script Expand / Collapse
Author
Message
Posted Thursday, May 29, 2014 11:45 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, May 30, 2014 8:51 AM
Points: 8, Visits: 27
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.
Post #1575730
Posted Thursday, May 29, 2014 11:54 AM


SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Today @ 11:27 AM
Points: 4,046, Visits: 9,201
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.



Luis C.
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

Forum Etiquette: How to post data/code on a forum to get the best help
Post #1575736
Posted Thursday, May 29, 2014 12:23 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, May 30, 2014 8:51 AM
Points: 8, Visits: 27
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?
Post #1575766
Posted Thursday, May 29, 2014 12:53 PM This worked for the OP Answer marked as solution


SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Today @ 11:27 AM
Points: 4,046, Visits: 9,201
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;




Luis C.
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

Forum Etiquette: How to post data/code on a forum to get the best help
Post #1575788
Posted Thursday, May 29, 2014 2:05 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, May 30, 2014 8:51 AM
Points: 8, Visits: 27
Got it Luis, I appreciate your help.
Post #1575838
Posted Thursday, May 29, 2014 2:16 PM


SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Today @ 11:27 AM
Points: 4,046, Visits: 9,201
You're welcome.
I hope that you could understand it and learn something new.



Luis C.
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

Forum Etiquette: How to post data/code on a forum to get the best help
Post #1575847
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse