Multiple statement conditions using OR AND within the query
-
20-02-2021 - |
Domanda
I have this survey table and I'm trying to gather the information of userid who answer Strongly agree to the question of I'm trying to watch my weight OR I love drinking Coffee Black AND must have answer Agree OR Neutral OR Strongly Disagree to these questions I thrive when a deadline is close and I'm all about trying something new.
UserId QUESTION Answer
1010 I am trying to watch my weight Strongly Agree
1120 I thrive when a deadline is close Strongly Agree
1120 I'm all about trying something new Neutral
2025 I'm all about trying something new Agree
3024 I love drinking Coffee Black Strongly Disagree
3024 I'm all about trying something new Agree
The queries that I tried to write keeps returning the questions that I didn't write.This might not be the best way write such query but I'm learning on how to properly write those. Any help would be appreciated.
SELECT userid_
,QUESTION
,answer
FROM [Survey_2019]]
Where QUESTION = 'I'm trying to watch my weight' AND
Answer = 'Strongly agree'
OR
QUESTION = 'I love drinking Coffee Black
AND
Answer = 'Strongly agree'
AND
QUESTION= 'I thrive when a deadline is close'
AND ANSWER= 'Agree'
OR 'Neutral' OR 'Strongly Disagree'
Soluzione
I have this survey table and I'm trying to gather the information of userid who answer Strongly agree to the question of I'm trying to watch my weight OR I love drinking Coffee Black AND must have answer Agree OR Neutral OR Strongly Disagree to these questions I thrive when a deadline is close and I'm all about trying something new.
Simplified:
[ A:a ON (Q:a/b) ]
AND [ (A:b/c/d) ON Q:c ]
AND [ (A:b/c/d) ON Q:d ]
In SQL:
SELECT UserId
FROM Survey_2019
GROUP BY UserId
HAVING SUM (CASE WHEN Question IN ('a', 'b') AND Answer = 'a' THEN 1 END) > 0
AND SUM (CASE WHEN Question = 'c' AND Answer IN ('b', 'c', 'd') THEN 1 END) > 0
AND SUM (CASE WHEN Question = 'd' AND Answer IN ('b', 'c', 'd') THEN 1 END) > 0
If you need separate records matched then use this SQL as CTE/subquery and select separate records from another copy of source table.
UPDATE
It seems that last 2 conditions may be combined into one using simplified condition
AND [ (A:b/c/d) ON Q:c/d ]
But the problem is to ensure that all (2 in that case) questions are mentioned. Yes, this is possible and can be performed using, for example
AND COUNT(DISTINCT CASE WHEN Question IN ('c','d')
AND Answer IN ('b', 'c', 'd')
THEN Question
END) = 2
If you need to ensure that there is no a record with the question(s) in interest and the answer not in list, then use additional condition similar to
AND COUNT(CASE WHEN Question IN ('c','d')
AND Answer NOT IN ('b', 'c', 'd')
THEN 1
END) = 0
Altri suggerimenti
For a start: You are missing a number of parentheses (in any case).
Then: You need to combine different records into a single result. One way has been shown by @Akina. Alternatively, you can run against two copies of the same table:
WITH
Part1 AS (
SELECT
userID
, question
, answer
FROM Survey_2019
WHERE
question IN ('I am trying to watch my weight', 'I love drinking Coffee Black')
AND
answer = 'Strongly Agree'
),
Part2 AS (
SELECT
userID
, question
, answer
FROM Survey_2019
WHERE
question IN ('I thrive when a deadline is close', 'I''m all about trying something new')
AND
answer IN ('Agree', 'Neutral', 'Strongly Disagree')
),
Overview (userID, question1, answer1, question2, answer2) AS (
SELECT
Part1.userID
, Part1.question
, Part1.answer
, Part2.question
, Part2.answer
FROM Part1
JOIN Part2
ON Part1.userID = Part2.userID
)
SELECT
* FROM Overview ORDER BY userID, question1, question2
;
See it in action: SQL Fiddle (Two records have been added to your sample data for an actual match.)
Akina's approach as detailed is more appropriate, if you care primarily for a list of users, for which the conditions are met. (But you can, of course, extend it along Akina's suggestion.) Mine provides directly all the details - but might become hard to swallow, if your data might be such as to produces multiple matches for a some user(s).
There is, however, a difference in Akina's an my reading of what is in Part2 of my approach: Akina expects an answer to both of the questions, I'd be satisfied, if at least one of the was answered. - You'll know best, which one is correct. (Both SQL statements can be adjusted accordingly.)
Your table design is probably/hopefully still under development… ;-)
Please comment, if and as this requires adjustment / further detail.