1

I have table with

+------------+-------+-------+
| tran_value | date  | name  |
+------------+-------+-------+
|         10 | 22may | mark  |
|         15 | 23may | mark  |
|         16 | 24may | mark  |
|        300 | 19jun | john  |
|        400 | 20jun | john  |
|        500 | 21jun | john  |
|        600 | 22jun | john  |
|        150 | 12dec | chris |
|        160 | 13dec | chris |
|        170 | 14dec | chris |
|        180 | 15dec | chris |
+------------+-------+-------+

and i tried to add outlier in a new column but it is throwing error

select date,tran_value,
case name
when name ="mark" and tran_value <=15 then new_trn =NULL
  else tran_value
when name ="john" and tran_value <400 then new_trn =NULL
  else tran_value
when name ="chris" and tran_value <170 then new_trn =NULL
  else tran_value

ii.e

+------------+-------+-------+
| tran_value | date  | name  |
+------------+-------+-------+
| NULL       | 22may | mark  |
| NULL       | 23may | mark  |
| 16         | 24may | mark  |
| NULL       | 19jun | john  |
| NULL       | 20jun | john  |
| 500        | 21jun | john  |
| 600        | 22jun | john  |
| NULL       | 12dec | chris |
| NULL       | 13dec | chris |
| 170        | 14dec | chris |
| 180        | 15dec | chris |
+------------+-------+-------+

any modifications in code.... thanks in advance

1 Answer 1

2
SELECT date,tran_value,CASE 
        WHEN name ="mark" and tran_value <=15 THEN new_trn =NULL         
        WHEN name ="john" and tran_value <400 THEN new_trn =NULL         
        WHEN name ="chris" and tran_value <170 THEN new_trn =NULL
        ELSE tran_value
END AS new_column;
Sign up to request clarification or add additional context in comments.

2 Comments

Remove name after CASE or alternatively the name = after the WHEN.
Thanks a lot @user ,@fancypants

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.