2

I have a query that where i need to put condition when case statement is true.I have tried like this but not geeting the correct value.

SELECT
    name,count(CASE WHEN date_part('year',time_stamp) = 2016 THEN answ_count end) AS Year15 
FROM 
        companies companies 
where 

    (CASE when no_answer='f' then value_s IS  not  NULL or value_n IS  not  NULL end )
1
  • That's a case expression, not a case statement... Commented Apr 6, 2016 at 6:28

2 Answers 2

3
SELECT name,
        count(CASE WHEN date_part('year',time_stamp) = 2016 THEN answ_count end) AS Year15 
FROM 
        companies companies 
where 
    CASE when no_answer='f' then value_s ELSE '1' end IS  not  NULL
    OR  CASE when no_answer='f' then value_n  ELSE '1' end IS  not  NULL 

CASE is an expression, you can only specify a value after the THEN part, not a condition like you did THEN value_s IS NOT NULL

Sign up to request clarification or add additional context in comments.

5 Comments

What are you expecting to get?
And also this error ERROR: CASE types integer and text cannot be matched LINE 27: ... and( CASE when no_answer='f' then dp_valu...
getting no error but result is same as first query that i have posted
Then I'll ask you again, what do you get? and what do you expect to get?
0

You can't use case like that.

It's hard to figure out what you are trying to do, but test the result of the case, rather than putting the test inside the case, like this:

...
where CASE when no_answer='f' then value_s else value_n end
  IS not NULL

1 Comment

Condition i want to add is like when no_answer='f' then it should check whether value_s or value_n is not null elase it should not check for that condition

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.