I have a query which will sum up some fields of a table based on specified date range provided by the user.
My query is as follows:
Select sum(field1),sum(field2) from table1
where (date1 between fromdate and todate) or (date2 between fromdate and todate).
Since I have used or Keyword, if date1 falls in the date range and date2 falls out of range then the sum of both the fields would be calculated which is wrong.
Is there any way to use if condition, which would make the field2 as 0 based on date2 falls out of range and in this case only sum of field1 would get calculated.
I know this can be done through 2 seperate queries but is there any way to do it in a single query.
My query is as follows:
Select sum(field1),sum(field2) from table1
where (date1 between fromdate and todate) or (date2 between fromdate and todate).
Since I have used or Keyword, if date1 falls in the date range and date2 falls out of range then the sum of both the fields would be calculated which is wrong.
Is there any way to use if condition, which would make the field2 as 0 based on date2 falls out of range and in this case only sum of field1 would get calculated.
I know this can be done through 2 seperate queries but is there any way to do it in a single query.