DROP TABLE IF EXISTS my_table;
CREATE TABLE my_table
(date DATE NOT NULL
,val CHAR(1) NOT NULL
,PRIMARY KEY (date)
);
INSERT INTO my_table VALUES
('2014-08-01','A'),
('2014-08-02','B'),
('2014-08-03','A'),
('2014-08-04','A'),
('2014-08-05','B'),
('2014-08-06','B'),
('2014-08-07','A'),
('2014-08-08','A'),
('2014-08-09','B'),
('2014-08-10','A'),
('2014-08-11','A');
SELECT MIN(date) min_date
, MAX(date) max_date
, SUM(val = 'A') A
, SUM(val = 'B') B
FROM my_table
GROUP
BY CEILING(TO_DAYS(date)/5)*5;
+------------+------------+------+------+
| min_date | max_date | A | B |
+------------+------------+------+------+
| 2014-08-01 | 2014-08-05 | 3 | 2 |
| 2014-08-06 | 2014-08-10 | 3 | 2 |
| 2014-08-11 | 2014-08-11 | 1 | 0 |
+------------+------------+------+------+
要快速解決,您應該快速編輯並格式化您的問題。 btw是什麼問題? – deterministicFail 2014-09-01 07:24:03
如何在輸出表中確定MIN和MAX? – ydoow 2014-09-01 07:27:47
@ydoow我編輯了我的問題 – aparna 2014-09-01 07:33:19