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'

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

4

3 回答 3

3

你可以

GROUP BY `date` 

查看每天的总和

于 2013-10-10T14:45:51.210 回答
1

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

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

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

SELECT SUM(x.days)
FROM (
    SELECT TIMESTAMPDIFF(DAY, startDate, endDate) AS days
    FROM fulfilment 
) x
GROUP BY employee.employee_id
于 2013-10-10T14:45:05.883 回答
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)
于 2013-10-10T14:53:54.263 回答