0

how to limit an integer query result to 1. a return of 2 to be 1, a return 1 to be 1, and a return of 0.5 to be 0.5 because it is <= 1. i don't want to modify the tables, i just want to modify the results.

This is my exact query.

select ((select "V01" from sports where "UID" = '1') * 1.0 ) / 
(select "V01" from master where "BALL" = 'REQUIREMENT') ;

I'm using postgres.

1
  • Just a quick note that you can't return 0.5 in an integer. You'll need to be casting to something else like numeric to get that. Commented Aug 2, 2012 at 17:06

3 Answers 3

2

To limit, you'd do something like this:

select 
    case 
        when yourNumber >= 1 then 1
        else yourNumber
    end
...

Then you just apply this concept to your query.

As noted by Wiseguy, you could also do:

select LEAST(yourNumber, 1)

, since this is postgresql.

The first solution will work with any ANSI SQL compatible database.

Update

Applied to your query, I think (if I understood what you want correctly) it would be like this:

select LEAST(1,
               ((select "V01" from sports where "UID" = '1') * 1.0 ) / 
               (select "V01" from master where "BALL" = 'REQUIREMENT')
       );
Sign up to request clarification or add additional context in comments.

5 Comments

How about LEAST(number, 1)?
@shahkalpesh I saw that. But it's kind of pointless though, don't you think?
@Adrian: Yes. I am sorry, I get it now. Edited.
Thanks LEAST did it and thank you for giving me the exact syntax!
Glad to help. Don't forget to upvote/accept the answer the helped you the most.
1

use the LEAST function , docs: http://www.postgresql.org/docs/8.3/static/functions-conditional.html. Also, check out GREATEST too

SELECT LEAST(1, <your value>)

EDIT replaced GREATEST with LEAST

2 Comments

I really think you mean LEAST
yeah, I probably meant LEAST :). Thanks. I was quick and lazy.
0

try this:

select CASE 
         WHEN ((select V01 from sports where UID = '1') * 1.0 ) / 
               (select V01 from master where BALL = 'REQUIREMENT') >= 1
         THEN 1
         ELSE ((select V01 from sports where UID = '1') * 1.0 ) / 
               (select V01 from master where BALL = 'REQUIREMENT')
       END;

Comments

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.