Results 1 to 3 of 3
  1. #1
    Join Date
    Dec 2016
    Posts
    19

    Answered: join query bringing back null values

    Hello,

    I am trying to do a join that isn't bringing in the values as I'm expect. I'd appreciate some help figuring out why.

    Here's the query:

    SELECT
    PS.ProjectId,
    PS.ProjectSafeguardId,
    PS.Description,
    SUM(S.Credit) AS TotalCredit
    FROM ProjectSafeguard PS
    left JOIN Safeguard S ON PS.ProjectSafeguardId = S.SafeguardId
    WHERE PS.ProjectId = 28
    GROUP BY
    PS.ProjectId,
    PS.Description,
    PS.ProjectSafeguardId

    The results I get look like this:

    Click image for larger version. 

Name:	join query results.png 
Views:	1 
Size:	122.5 KB 
ID:	17279

    The unexpected values I'm getting are the nulls under TotalCredit. They should not be null.

    To test this, I picked the top ProjectSafeguardId of 1423 and ran this on the Safeguard table using:

    SELECT
    [ProjectSafeguardId],
    [Credit]
    FROM [RiskAliveDev].[dbo].[Safeguard]
    where ProjectSafeguardId = 1423

    and got:

    Click image for larger version. 

Name:	safeguard query results.png 
Views:	1 
Size:	26.7 KB 
ID:	17280

    So we have 10 Safeguards linked to ProjectSafeguard 1423, all with Credits 1.000, but their sum is coming through as null in the join query.

    What am I doing wrong?

  2. Best Answer
    Posted by MCrowley

    "Check your join:
    Code:
    FROM ProjectSafeguard PS
    left JOIN Safeguard S ON PS.ProjectSafeguardId = S.SafeguardId
    Did you mean S.ProjectSafeguardId instead?"


  3. #2
    Join Date
    Jan 2003
    Location
    Massachusetts
    Posts
    5,858
    Provided Answers: 17
    Check your join:
    Code:
    FROM ProjectSafeguard PS
    left JOIN Safeguard S ON PS.ProjectSafeguardId = S.SafeguardId
    Did you mean S.ProjectSafeguardId instead?

  4. #3
    Join Date
    Dec 2016
    Posts
    19
    Quote Originally Posted by MCrowley View Post
    Check your join:
    Code:
    FROM ProjectSafeguard PS
    left JOIN Safeguard S ON PS.ProjectSafeguardId = S.SafeguardId
    Did you mean S.ProjectSafeguardId instead?
    d'Oh! Yes, I did. Amazing how little things like that just don't catch your eye (well, my eye anyway).

    Thanks!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •