4

I would like to do something like this:

select 
case when (select count(*) as score from users t1 )   >5   THEN score   else 0 end 

When i try it i get error:

column score doesn't exists. 

Can i do this in some other way? I need it to set a LIMIT value. I would like to do it of course in this way:

select 
case when (select count(*) as score from users t1 )   >5   THEN (select count(*) as score from users)    else 0 end 

but than I need execute two times this same query. Have someone some ideas?

1
  • What about common table expression? It could help you. Commented Jun 23, 2013 at 20:58

1 Answer 1

8

You can use WITH clause:

with a as (select count(*) score from t)
select case when score > 5 then score else 0 end from a;

Or subquery (inline-view):

select case when score > 5 then score else 0 end 
from (select count(*) score from t) t;
Sign up to request clarification or add additional context in comments.

1 Comment

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.