sqldataexplorer

Getting all questions that do not necessarily have two different tags using SEDE


So I am trying to use SEDE to get a table of questions that contain two separate tags (but do not necessarily have both) from Puzzling Stack Exchange to find puzzles that I might be able to solve. After I managed to fix my previous code, I was able to include the other tag that I am trying to restrict the list of questions to.

However, I am having a hard time writing the code so that I can retrieve all of the questions that either only have one or both of the tags. Instead, I am only able to retrieve the questions that have both.

Here is the code that I have managed to come up with (after reading a few more tutorials):

SELECT p.Title, p.Id, p.Score, p.ViewCount, u.DisplayName, p.Tags
FROM Posts p
JOIN Users u ON p.OwnerUserId = u.Id
INNER JOIN PostTags pt ON p.Id=p.PostId
INNER JOIN Tags t ON t.id = pt.TagId
INNER JOIN PostTags pt1 ON p.Id=pt1.PostId
INNER JOIN Tags t1 ON t1.Id = pt1.TagId
WHERE p.PostId=1
AND (t.TagName=##tagName:string?logical-deduction##)
AND (t1.TagName=##tagName:string?grid-deduction##)
AND p.Score <=10
AND p.Score >=0
AND p.AcceptedAnswerId is null
ORDER BY p.Score DESC

which only seems to return questions that contain both tags (along with whatever other tags they have) while also abiding by the other restrictions.

My question is: Why is it only returning questions with both of the tags, and how can I make it so the query also returns the questions that only have one of the following tags?


Solution

  • Here is how I would personally solve the problem:

    SELECT p.Title, p.Id, p.Score, p.ViewCount, u.DisplayName, p.Tags
    FROM Posts p
    JOIN Users u ON p.OwnerUserId = u.Id
    INNER JOIN PostTags pt ON p.Id=pt.PostId
    INNER JOIN Tags t ON t.id = pt.TagId
    WHERE p.PostTypeId=1
    AND (t.TagName=##tagName:string?logical-deduction## OR t.TagName=##tagName:string?grid-deduction##)
    AND p.Score <=10
    AND p.Score >=0
    AND p.AcceptedAnswerId is null
    ORDER BY p.Score DESC
    

    Noted differences: got rid of duplicate joins t1 pt1, this was causing your query to not work as expected. I added a search on p.Tags so if, in the future, you want to get rid of forcing the t.TagName to be either grid-deduction or logical-deduction, you could comment out that line, uncomment out the other, and this would just leave p.Tags being searched. This would cause the query to not care about the main tag and just the additional tags.

    If you wanted to ONLY return values that have grid or logical (but not both) you could use the following where clause:

    AND (
        (p.Tags LIKE '%logical-deduction%' AND p.Tags NOT LIKE '%grid-deduction%') OR
        (p.Tags LIKE '%grid-deduction%' AND p.Tags NOT LIKE '%logical-deduction%')
    )