0

please help me with this query.. It seems simple but read below to see: Query:

select SUM(price) 
from prices_adverts 
where advert_id="1" 
  and room_type_id="2" 
  and (date >= "2013-10-10" 
  AND date <"2013-10-13") 
order by price    

this query checks for price for hotel room for period between above dates. In this case for 2 days - 2013-10-10 and 2013-10-11. I have setted in the database price for 2013-10-10 - 34 euro and price 2013-10-11 - 0.00euro.

How I can make the query to return 0.00 price if even 1 day in the period has price 0.00?

I use this 0.00 value to show later that the room is not available for the specific date.. Now this query returns eur 34 as sum which is the price only for 1 day 2013-10-10 because the second day the room is not available..

I hope you understand the nature of the issue

3
  • There's an aggregating function but no GROUP BY clause. Shurely shome mishtake. Commented Oct 10, 2013 at 8:03
  • @Strawberry you can have a agg func without no grouping ... but the order by makes no sense Commented Oct 10, 2013 at 8:05
  • order by is indeed not needed. Neither is grouping - when you don't have one it just performs a sum, not a sum per group, no issue here Commented Oct 10, 2013 at 8:14

1 Answer 1

5
select if(min(price)=0, 0, SUM(price) )
from prices_adverts 
where advert_id="1" 
  and room_type_id="2" 
  and (date >= "2013-10-10" 
  AND date <"2013-10-13") 
order by price 
Sign up to request clarification or add additional context in comments.

3 Comments

if you use case when instead of if, it will work on other databases as well.
AdrianBR - you saved my day man ! Genious ! Thanks, I accept your answer and will vote for it in 3 minutes :)
@Quandary 'if' is much more cleaner visually, I like to use that for mysql. It's a statement designed for a single option case.

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.