0

I want to select the sum of values of my_field. The unit of my_field is in seconds. I don't want to exceed 24 hours, so when it exceeds 24 hours, I want it to select 24*60*60 = 86400.

SELECT IF(SUM(my_field) > 86400, 86400, SUM(my_field))
...

This solution doesn't seem to work. What else can I do?

2 Answers 2

3

Using least would be much easier:

SELECT LEAST(SUM(my_field), 86400)
FROM   my_table
Sign up to request clarification or add additional context in comments.

5 Comments

Not SMALLEST... (or is it LEAST?)
Thanks for the answers, this gave me the idea.
@jarlh yup, that was a momentarily lapse in concentration. Indeed - least
@StuartLC yup - wasn't concentrating. See latest edit.
@tolga note that as noted in the above comment, it should have been least, not greatest (edited to update the answer).
2

Another alternative is to use CASE expression like

SELECT CASE WHEN SUM(my_field) > 86400 THEN 86400 ELSE SUM(my_field) END
FROM table1

1 Comment

Thank you, this answer is useful too.

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.