How to use if(IFF) condition in a query

bibek24

New member
Local time
Today, 07:33
Joined
Sep 26, 2008
Messages
7
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.
 
Are you trying to sum Field1 based on date1 and field2 based on date2?
then
select sum(iif(date1 between fromdate and todate,field1,0),sum(iif(date2 between fromdate and todate,field2,0) from table1

Brian
 
Thanks Brian, it worked.
 

Users who are viewing this thread

Back
Top Bottom