想詢問各位MYSQL語法高手
要如何在一個table中,把指定日期間內,
各學號 使用數量總和 與 最大使用
的查詢方法?
+----------------TABLE-------------+
+--------------------+--------------+-------------+
| 日期 | 學號| 使用數量|
+--------------------+--------------+-------------+
| 2014-08-07 10:00:00| 10001 | 2 |
| 2014-08-07 11:00:00| 10001 | 4 |
| 2014-08-08 12:00:00| 10001 | 6 |
| 2014-08-09 01:00:00| 10002 | 8 |
| 2014-08-10 10:00:00| 10002 | 10 |
| 2014-08-12 18:00:00| 10002 | 12 |
+--------------------+--------------+-------------+
以下是小弟腦補構想的結果:
BETWEEN '2014-08-07' AND '2014-08-10'
--------------+-------------+-------------+
學號| 使用數量| 最大數量|
--------------+-------------+-------------+
10001 | 12 | 6|
10002 | 18 | 8|
--------------+-------------+-------------+
<pre class="c" name="code">CREATE TABLE ithelp0813(
id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
itime DATETIME NOT NULL,
stdid CHAR(5) NOT NULL,
amount SMALLINT UNSIGNED NOT NULL
);
INSERT INTO ithelp0813(itime, stdid, amount) VALUES
('2014-08-07 10:00:00', 10001, 2),
('2014-08-07 11:00:00', 10001, 4),
('2014-08-08 12:00:00', 10001, 6),
('2014-08-09 01:00:00', 10002, 8),
('2014-08-10 10:00:00', 10002, 10),
('2014-08-12 18:00:00', 10002, 12);
--
SELECT stdid AS '學號'
, SUM(amount) AS '使用數量'
, MAX(amount) AS '最大數量'
FROM ithelp0813
WHERE DATE(itime)
BETWEEN '2014-08-07'
AND '2014-08-10'
GROUP BY stdid
ORDER BY stdid;
+--------+--------------+--------------+
| 學號 | 使用數量 | 最大數量 |
+--------+--------------+--------------+
| 10001 | 12 | 6 |
| 10002 | 18 | 10 |
+--------+--------------+--------------+