1

我有一张像这样的桌子:

Status | Quantity | PeriodDate
------------------------------
100    | 2        | 2013-07-02
100    | 3        | 2013-07-02
100    | 5        | 2013-07-02
100    | 3        | 2013-07-03
100    | 1        | 2013-07-03
100    | 0        | 2013-07-04
100    | 0        | 2013-07-04
100    | 5        | 2013-07-04
100    | 6        | 2013-07-05
100    | 4        | 2013-07-06
100    | 8        | 2013-07-06

而且我需要选择数据,以便它显示一个范围内每个不同日期的数量计数。我原以为我可以做一些事情:

SELECT DISTINCT PeriodDate, SUM(Quantity) as TotalQuantity where PeriodDate BETWEEN '2013-07-02' AND '2013-07-04'

但这会返回一行,其中包含所有数量的全部总和。

我追求的是:

TotalQuantity | PeriodDate
--------------------------
10            | 2013-07-02
4             | 2013-07-03
5             | 2013-07-04
6             | 2013-07-05

我如何实现这一目标?

4

2 回答 2

4
SELECT PeriodDate, 
       SUM(Quantity) as TotalQuantity 
from your_table
where PeriodDate BETWEEN '2013-07-02' AND '2013-07-05'
group by PeriodDate 
于 2013-10-04T14:18:18.333 回答
3

Distinct 适用于PeriodDateTotalQuantityDISTINCT您需要使用 ,而不是使用GROUP BY

SELECT PeriodDate, SUM(Quantity) as TotalQuantity 
FROM ???
WHERE PeriodDate BETWEEN '2013-07-02' AND '2013-07-04'
GROUP BY PeriodDate

说明: Distinct 用于消除结果集中的重复行。

进一步解释:http ://www.mysqltutorial.org/mysql-distinct.aspx

于 2013-10-04T14:21:27.033 回答