我的表格設計用於包含寵物及其活動數據,例如睡眠時間和鍛鍊時間。表格定義如下。連續數天MySQL羣組和聚集
CREATE TABLE IF NOT EXISTS TEST_ACTIVITY(
pet_id INT(11) UNSIGNED NOT NULL,
simple_sleep_time INT(11),
deep_sleep_time INT(11),
mild_movement_time INT(11),
moderate_movement_time INT(11),
severe_movement_time INT(11),
start_time INT(11),
date DATE,
PRIMARY KEY (pet_id, start_time, date)
) ENGINE=INNODB
將以下數據插入表中。
INSERT INTO TEST_ACTIVITY (pet_id, simple_sleep_time, deep_sleep_time,mild_movement_time, moderate_movement_time, severe_movement_time, start_time, date)
VALUES
(100, 1, 1, 1, 1, 1, 0, '2015-03-10'),
(100, 2, 1, 1, 1, 1, 30, '2015-03-10'),
(100, 3, 1, 1, 1, 1, 0, '2015-03-11'),
(100, 4, 1, 1, 1, 1, 30, '2015-03-11'),
(100, 5, 1, 1, 1, 1, 0, '2015-03-12'),
(100, 6, 1, 1, 1, 1, 30, '2015-03-12'),
(100, 7, 1, 1, 1, 1, 0, '2015-03-13'),
(100, 8, 1, 1, 1, 1, 30, '2015-03-13'),
(101, 9, 1, 1, 1, 1, 0, '2015-03-10'),
(101, 10, 1, 1, 1, 1, 30, '2015-03-10'),
(101, 11, 1, 1, 1, 1, 0, '2015-03-11'),
(101, 12, 1, 1, 1, 1, 30, '2015-03-11'),
(101, 13, 1, 1, 1, 1, 0, '2015-03-12'),
(101, 14, 1, 1, 1, 1, 30, '2015-03-12'),
(101, 15, 1, 1, 1, 1, 0, '2015-03-13'),
(101, 16, 1, 1, 1, 1, 30, '2015-03-13'),
(102, 17, 1, 1, 1, 1, 0, '2015-03-10'),
(102, 18, 1, 1, 1, 1, 30, '2015-03-10'),
(102, 19, 1, 1, 1, 1, 0, '2015-03-11'),
(102, 20, 1, 1, 1, 1, 30, '2015-03-11'),
(102, 21, 1, 1, 1, 1, 0, '2015-03-12'),
(102, 22, 1, 1, 1, 1, 30, '2015-03-12'),
(102, 23, 1, 1, 1, 1, 0, '2015-03-13'),
(102, 24, 1, 1, 1, 1, 30, '2015-03-13');
select * from TEST_ACTIVITY ORDER BY pet_id, date, start_time;
+--------+-------------------+-----------------+--------------------+------------------------+----------------------+------------+------------+
| pet_id | simple_sleep_time | deep_sleep_time | mild_movement_time | moderate_movement_time | severe_movement_time | start_time | date |
+--------+-------------------+-----------------+--------------------+------------------------+----------------------+------------+------------+
| 100 | 1 | 1 | 1 | 1 | 1 | 0 | 2015-03-10 |
| 100 | 2 | 1 | 1 | 1 | 1 | 30 | 2015-03-10 |
| 100 | 3 | 1 | 1 | 1 | 1 | 0 | 2015-03-11 |
| 100 | 4 | 1 | 1 | 1 | 1 | 30 | 2015-03-11 |
| 100 | 5 | 1 | 1 | 1 | 1 | 0 | 2015-03-12 |
| 100 | 6 | 1 | 1 | 1 | 1 | 30 | 2015-03-12 |
| 100 | 7 | 1 | 1 | 1 | 1 | 0 | 2015-03-13 |
| 100 | 8 | 1 | 1 | 1 | 1 | 30 | 2015-03-13 |
| 101 | 9 | 1 | 1 | 1 | 1 | 0 | 2015-03-10 |
| 101 | 10 | 1 | 1 | 1 | 1 | 30 | 2015-03-10 |
| 101 | 11 | 1 | 1 | 1 | 1 | 0 | 2015-03-11 |
| 101 | 12 | 1 | 1 | 1 | 1 | 30 | 2015-03-11 |
| 101 | 13 | 1 | 1 | 1 | 1 | 0 | 2015-03-12 |
| 101 | 14 | 1 | 1 | 1 | 1 | 30 | 2015-03-12 |
| 101 | 15 | 1 | 1 | 1 | 1 | 0 | 2015-03-13 |
| 101 | 16 | 1 | 1 | 1 | 1 | 30 | 2015-03-13 |
| 102 | 17 | 1 | 1 | 1 | 1 | 0 | 2015-03-10 |
| 102 | 18 | 1 | 1 | 1 | 1 | 30 | 2015-03-10 |
| 102 | 19 | 1 | 1 | 1 | 1 | 0 | 2015-03-11 |
| 102 | 20 | 1 | 1 | 1 | 1 | 30 | 2015-03-11 |
| 102 | 21 | 1 | 1 | 1 | 1 | 0 | 2015-03-12 |
| 102 | 22 | 1 | 1 | 1 | 1 | 30 | 2015-03-12 |
| 102 | 23 | 1 | 1 | 1 | 1 | 0 | 2015-03-13 |
| 102 | 24 | 1 | 1 | 1 | 1 | 30 | 2015-03-13 |
+--------+-------------------+-----------------+--------------------+------------------------+----------------------+------------+------------+
我想用這個公式先計算每個寵物的日分數:score = SUM(severe_movement_time) + SUM(moderate_movement_time) + SUM(mild_movement_time) + SUM(simple_sleep_time)
,然後再決定根據其得分每個寵物的等級。通過使用以下查詢,我可以在2015-03-10的一天內完成。
SELECT pet_id, date, score, rank
FROM
(
SELECT t.pet_id, t.date, t.score, @prev := @curr, @curr := score, @rank := IF(@prev = @curr, @rank, @rank+1) AS rank
FROM (
SELECT pet_id, date, COALESCE(SUM(simple_sleep_time), 0) shallow,
COALESCE(SUM(deep_sleep_time), 0) deep, COALESCE(SUM(mild_movement_time), 0) light,
COALESCE(SUM(moderate_movement_time), 0) moderate, COALESCE(SUM(severe_movement_time), 0) heavy,
(COALESCE(SUM(severe_movement_time), 0) + COALESCE(SUM(moderate_movement_time), 0) + COALESCE(SUM(mild_movement_time), 0) + COALESCE(SUM(simple_sleep_time), 0)) score
FROM TEST_ACTIVITY
WHERE date = DATE('2015-03-10')
GROUP BY pet_id
ORDER BY score DESC
) t, (SELECT @curr := null, @prev := null, @rank := 0) r ORDER BY score DESC
) x
我的問題是如何編寫一個查詢來計算分數和排名連續數天,例如4天(起始= 2015年3月10日日期,結束日期= 2015年3月13日)。預期結果如下所示。
| pet_id | date | score | rank |
+--------+------------+-------+------+
| 102 | 2015-03-10 | 41 | 1 |
| 101 | 2015-03-10 | 25 | 2 |
| 100 | 2015-03-10 | 9 | 3 |
| 102 | 2015-03-11 | 45 | 1 |
| 101 | 2015-03-11 | 29 | 2 |
| 100 | 2015-03-11 | 13 | 3 |
| 102 | 2015-03-12 | 49 | 1 |
| 101 | 2015-03-12 | 33 | 2 |
| 100 | 2015-03-12 | 17 | 3 |
| 102 | 2015-03-13 | 53 | 1 |
| 101 | 2015-03-13 | 37 | 2 |
| 100 | 2015-03-13 | 21 | 3 |
認真考慮修改您的設計 – Strawberry
只是按GROUP BY pet_id組織,日期 – Cherif