Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Unable to figure out SQL Script


Unable to figure out SQL Script

Author
Message
neddyflanders1
neddyflanders1
Forum Newbie
Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)

Group: General Forum Members
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.
Luis Cazares
Luis Cazares
SSCrazy Eights
SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)

Group: General Forum Members
Points: 8549 Visits: 18141
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.
General Disclaimer:
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?


How to post data/code on a forum to get the best help: Option 1 / Option 2
neddyflanders1
neddyflanders1
Forum Newbie
Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)

Group: General Forum Members
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?
Luis Cazares
Luis Cazares
SSCrazy Eights
SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)

Group: General Forum Members
Points: 8549 Visits: 18141
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.
General Disclaimer:
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?


How to post data/code on a forum to get the best help: Option 1 / Option 2
neddyflanders1
neddyflanders1
Forum Newbie
Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)

Group: General Forum Members
Points: 8 Visits: 27
Got it Luis, I appreciate your help.
Luis Cazares
Luis Cazares
SSCrazy Eights
SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)

Group: General Forum Members
Points: 8549 Visits: 18141
You're welcome.
I hope that you could understand it and learn something new. ;-)


Luis C.
General Disclaimer:
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?


How to post data/code on a forum to get the best help: Option 1 / Option 2
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search