SELECT SUM mysql

时间:2013-10-10 14:43:16

标签: mysql sum

我需要为员工属于某个部门的每一天选择一个数据总和,我希望能够选择一个日期范围,并且每天都有这样的总结,这是我刚刚得到一个一个字段中整个日期范围的摘要。

这是我目前的询问

select 
  date, 
  SUM(no_of_live_products) 
from 
    fulfilment 
INNER JOIN 
  employee 
  ON  employee.employee_id = fulfilment.employee_id 
where 
      employee.department = 'Telecoms    Services' 
  and date >= '2013-09-16' 
  and date <= '2013-09-29'

是否可以获取该日期范围内每天的数据摘要?

3 个答案:

答案 0 :(得分:2)

你可以

GROUP BY `date` 

查看每天的总和

答案 1 :(得分:1)

如果您想获得天数并且您有多个时间范围,

首先,获取单个时间范围的日期数,然后将它们相加(需要一个子选择)。

以此为例,它不是一个完整的查询您的情况:

SELECT SUM(x.days)
FROM (
    SELECT TIMESTAMPDIFF(DAY, startDate, endDate) AS days
    FROM fulfilment 
) x
GROUP BY employee.employee_id

答案 2 :(得分:1)

为什么不试试

SELECT 
  date, 
  SUM(no_of_live_products) 
FROM 
    fulfilment 
INNER JOIN 
  employee ON  employee.employee_id = fulfilment.employee_id 
WHERE 
      employee.department = 'Telecoms    Services' 
  AND date >= '2013-09-16' 
  AND date <= '2013-09-29'
GROUP BY date(date)