简体   繁体   中英

Calculating values between two dates

i want to edit my query to sum values between two dates but to get the sum of each day. for example if two dates between "2020-01-01" and "2020-01-31" then i want the 31 value for each day. but in my query below it gives me the sum of the whole month

select SUM(`discounts`.`Amount`) 
from `manfz`.`discounts`
inner join `manfz`.`item_sales`
on (`discounts`.`Receipt` = `item_sales`.`Receipt` ) and  (`discounts`.`ProductName` = `item_sales`.`ProductName`)
where ( `item_sales`.`Date` >=  date1 +  And `item_sales`.`Date` <=  date2);

Assuming that you have a lest one record per day, you can add a group by clause to your query:

select date(s.date) dy, sum(d.amount) sum_amount
from manfz.discounts d
inner join manfz.item_sales s
    on d.receipt = s.receipt and  d.productname = s.productname
where s.date >=  date1 and s.date <=  date2
group by date(s.date)
order by dy;

Notes:

  • backticks are really needed where for identifier (column and table names) that contain special characters

  • parentheses are not necessary to separate and conditions

  • there is a strange + in your where clause that I would consider a typo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM