Skip to content

Instantly share code, notes, and snippets.

@gdoddsy
Last active October 2, 2018 21:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gdoddsy/8e061026066c26c831ed438430f861f1 to your computer and use it in GitHub Desktop.
Save gdoddsy/8e061026066c26c831ed438430f861f1 to your computer and use it in GitHub Desktop.
--Use a Where Clause
SELECT Posts.Title
,acceptedAnswer.CreationDate
,Users.DisplayName
FROM dbo.Posts
LEFT JOIN dbo.Posts AS acceptedAnswer
ON acceptedAnswer.Id = Posts.AcceptedAnswerId
LEFT JOIN dbo.Users
ON acceptedAnswer.LastEditorUserId = Users.Id
WHERE Posts.PostTypeId = 1
AND
( ( acceptedAnswer.Id IS NULL AND Users.Id IS NULL)
OR ( acceptedAnswer.Id IS NOT NULL AND Users .Id IS NOT NULL)
)
--Use a nested join
SELECT Posts.Title
,acceptedAnswer.CreationDate
,Users.DisplayName
FROM dbo.Posts
LEFT JOIN dbo.Posts AS acceptedAnswer
INNER JOIN dbo.Users
ON acceptedAnswer.LastEditorUserId = Users.Id
ON acceptedAnswer.Id = Posts.AcceptedAnswerId
WHERE Posts.PostTypeId = 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment