Unable to figure out SQL Script

  • 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.

  • 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
  • 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?

  • 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
  • Got it Luis, I appreciate your help.

  • 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

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply