简体   繁体   English

计算两个日期之间的值

[英]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.例如,如果“2020-01-01”和“2020-01-31”之间的两个日期,那么我想要每天的 31 值。 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:假设您每天最少有一条记录,您可以在查询中添加一个group by子句:

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不需要括号来分隔and条件

  • there is a strange + in your where clause that I would consider a typo你的where子句中有一个奇怪的+ ,我认为这是一个错字

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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