0

I have data in below table format.

id qid oid
1 101 10
2 101 20
3 103 10
4 102 20
5 101 10

case expression :

case when (qid=101 and oid=10) and (qid=103 and oid=10) then 1 else end as output

If above case condition match then output should be below.

id qid oid output
1 101 10 1
3 103 10 1
5 101 10 1
3
  • 2
    qid can't hold a value of 101 AND 103 simultaneously. You need an OR. case when (qid=101 and oid=10) OR (qid=103 and oid=10) Commented Jul 26, 2022 at 13:25
  • 1
    @JSpratt perhaps the q in qid means quantum .. so that would explain two states simultaneously :-D Commented Jul 26, 2022 at 13:30
  • Just to clarify my earlier point, your CASE statement in question is equal to this: CASE WHEN (qid = 101 AND qid = 103) AND oid = 10 THEN 1 END. Which means for any one record/row you would need qid to be 101 AND 103 which isn't possible based on your data. You can either use an OR or write it in multiple statements like peter suggested. Commented Jul 26, 2022 at 15:58

1 Answer 1

1

Try writing the case statement in more than 1 LINe i.e

CASE 
    WHEN (qid=101 and oid=10) THEN 1
    WHEN  (qid=103 and oid=10) then 1 
ELSE end as output
Sign up to request clarification or add additional context in comments.

2 Comments

And If both condition matched "1" like above solution THEN final result should be "1". is it possible?
Can you have a look at this sample link

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.