0

I am having a hard time figuring this query out. I need records from issueMaster based on userId and visible, joined to issueActionDetails that are visible, but there may not be any issueActionDetails. I am only getting issues that have actions.

SELECT
b.AssignedTo, 
b.Visible,
c.ONEKEY_ID,
C.PHYS_NAME[Doctor],
B.IssueDescription[Issue to Address],
CASE WHEN A.ActionDescription IS NULL THEN 'To Be Assigned' ELSE A.ActionDescription END as [Action],
CASE WHEN A.AssignedTo IS NULL THEN 'To Be Assigned' ELSE A.AssignedTo END as [Assigned To],
CASE WHEN D.[Description] IS NULL THEN 'To Be Assigned' ELSE D.[Description] END As [Status]
FROM [dbo].[tbl_IssueMaster] B
LEFT JOIN  [dbo].[tbl_IssueActionDetails] A ON A.IssueID =B.IssueID
INNER JOIN [dbo].[tbl_DoctorsNew] C ON B.OnekeyID =C.ONEKEY_ID
INNER JOIN  [dbo].[Action_Status] D ON A.ActionStatus = D.ID 
WHERE B.AssignedTo = @UserId AND B.Visible =1 AND A.Visible =1
ORDER BY c.ONEKEY_ID,B.DisplayOrder ,A.DisplayOrder 
2
  • 2
    Where is the problem with datetime and date ... I don't see what is your problem. Commented Aug 15, 2014 at 13:37
  • Your question body is not in the context what you said in question title. Commented Aug 15, 2014 at 13:40

2 Answers 2

1

The "outerness" of the join to tbl_IssueActionDetails is negated by two things:

The inner join to Action_Status:

INNER JOIN  [dbo].[Action_Status] D ON A.ActionStatus = D.ID 

And the predicate in the WHERE clause

AND A.Visible =1

For any rows returned where there isn't a matching row in tbl_IssueActionDetails, the values in the columns for that table will all be NULL. (The database is essentially creating a row from tbl_IssueActionDetails that consists of all NULLs, and "matching" that row to the row from the table on the left side.

Any predicate that excludes NULL values from columns in that table will exclude that row. If we specify to only return rows where "A.Visible=1", that will exclude any rows that have a NULL value in that column. Which is what I meant when I said the "outerness" of the LEFT JOIN operation was negated.


The fix is to move the predicate to the ON clause of the join to tbl_IssueActionDetail

and change that INNER JOIN to an outer join LEFT JOIN

Sign up to request clarification or add additional context in comments.

Comments

0
  LEFT JOIN [dbo].[tbl_IssueActionDetails] A
              ON A.IssueID = B.IssueID
                    AND A.Visible = 1

remove "AND A.Visible = 1" from the where clause, add it as a join condition instead

OR, alter the where clause to permit a NULL condition for the LEFT JOIN

WHERE B.AssignedTo = @UserId
      AND B.Visible = 1
      AND ( A.Visible = 1 OR A.Visible IS NULL)

If you always insist that A.Visible = 1 in the where clause you suppress the NULLs that a LEFT JOIN can provide. Be on guard for this whenever you use an outer join.

1 Comment

There's another predicate that also negates the outerness of the LEFT JOIN: the predicate in the INNER JOIN on A.IssueID.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.