2017-04-17 67 views
0

該表旨在包含寵物及其活動數據,例如睡眠時間和鍛鍊時間。表格定義如下。有關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) NOT NULL, 
    date DATE NOT NULL, 
    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, 60, '2015-03-10'), 
(100, 4, 1, 1, 1, 1, 90, '2015-03-10'), 
(100, 5, 1, 1, 1, 1, 120, '2015-03-10'), 
(100, 6, 1, 1, 1, 1, 150, '2015-03-10'), 
(100, 7, 1, 1, 1, 1, 0, '2015-03-11'), 
(100, 8, 1, 1, 1, 1, 30, '2015-03-11'), 
(100, 9, 1, 1, 1, 1, 60, '2015-03-11'), 
(100, 10, 1, 1, 1, 1, 90, '2015-03-11'), 
(100, 11, 1, 1, 1, 1, 120, '2015-03-11'), 
(100, 12, 1, 1, 1, 1, 150, '2015-03-11'), 
(100, 13, 1, 1, 1, 1, 0, '2015-03-12'), 
(100, 14, 1, 1, 1, 1, 30, '2015-03-12'), 
(100, 15, 1, 1, 1, 1, 60, '2015-03-12'), 
(100, 16, 1, 1, 1, 1, 90, '2015-03-12'), 
(100, 17, 1, 1, 1, 1, 120, '2015-03-12'), 
(100, 18, 1, 1, 1, 1, 150, '2015-03-12'), 
(100, 19, 1, 1, 1, 1, 0, '2015-03-13'), 
(100, 20, 1, 1, 1, 1, 30, '2015-03-13'), 
(100, 21, 1, 1, 1, 1, 60, '2015-03-13'), 
(100, 22, 1, 1, 1, 1, 90, '2015-03-13'), 
(100, 23, 1, 1, 1, 1, 120, '2015-03-13'), 
(100, 24, 1, 1, 1, 1, 150, '2015-03-13'), 
(101, 25, 1, 1, 1, 1, 0, '2015-03-10'), 
(101, 26, 1, 1, 1, 1, 30, '2015-03-10'), 
(101, 27, 1, 1, 1, 1, 60, '2015-03-10'), 
(101, 28, 1, 1, 1, 1, 90, '2015-03-10'), 
(101, 29, 1, 1, 1, 1, 120, '2015-03-10'), 
(101, 30, 1, 1, 1, 1, 150, '2015-03-10'), 
(101, 31, 1, 1, 1, 1, 0, '2015-03-11'), 
(101, 32, 1, 1, 1, 1, 30, '2015-03-11'), 
(101, 33, 1, 1, 1, 1, 60, '2015-03-11'), 
(101, 34, 1, 1, 1, 1, 90, '2015-03-11'), 
(101, 35, 1, 1, 1, 1, 120, '2015-03-11'), 
(101, 36, 1, 1, 1, 1, 150, '2015-03-11'), 
(101, 37, 1, 1, 1, 1, 0, '2015-03-12'), 
(101, 38, 1, 1, 1, 1, 30, '2015-03-12'), 
(101, 39, 1, 1, 1, 1, 60, '2015-03-12'), 
(101, 40, 1, 1, 1, 1, 90, '2015-03-12'), 
(101, 41, 1, 1, 1, 1, 120, '2015-03-12'), 
(101, 42, 1, 1, 1, 1, 150, '2015-03-12'), 
(101, 43, 1, 1, 1, 1, 0, '2015-03-13'), 
(101, 44, 1, 1, 1, 1, 30, '2015-03-13'), 
(101, 45, 1, 1, 1, 1, 60, '2015-03-13'), 
(101, 46, 1, 1, 1, 1, 90, '2015-03-13'), 
(101, 47, 1, 1, 1, 1, 120, '2015-03-13'), 
(101, 48, 1, 1, 1, 1, 150, '2015-03-13'), 
(102, 49, 1, 1, 1, 1, 0, '2015-03-10'), 
(102, 50, 1, 1, 1, 1, 30, '2015-03-10'), 
(102, 51, 1, 1, 1, 1, 60, '2015-03-10'), 
(102, 52, 1, 1, 1, 1, 90, '2015-03-10'), 
(102, 53, 1, 1, 1, 1, 120, '2015-03-10'), 
(102, 54, 1, 1, 1, 1, 150, '2015-03-10'), 
(102, 55, 1, 1, 1, 1, 0, '2015-03-11'), 
(102, 56, 1, 1, 1, 1, 30, '2015-03-11'), 
(102, 57, 1, 1, 1, 1, 60, '2015-03-11'), 
(102, 58, 1, 1, 1, 1, 90, '2015-03-11'), 
(102, 59, 1, 1, 1, 1, 120, '2015-03-11'), 
(102, 60, 1, 1, 1, 1, 150, '2015-03-11'), 
(102, 61, 1, 1, 1, 1, 0, '2015-03-12'), 
(102, 62, 1, 1, 1, 1, 30, '2015-03-12'), 
(102, 63, 1, 1, 1, 1, 60, '2015-03-12'), 
(102, 64, 1, 1, 1, 1, 90, '2015-03-12'), 
(102, 65, 1, 1, 1, 1, 120, '2015-03-12'), 
(102, 66, 1, 1, 1, 1, 150, '2015-03-12'), 
(102, 67, 1, 1, 1, 1, 0, '2015-03-13'), 
(102, 68, 1, 1, 1, 1, 30, '2015-03-13'), 
(102, 69, 1, 1, 1, 1, 60, '2015-03-13'), 
(102, 70, 1, 1, 1, 1, 90, '2015-03-13'), 
(102, 71, 1, 1, 1, 1, 120, '2015-03-13'), 
(102, 72, 1, 1, 1, 1, 150, '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 |   60 | 2015-03-10 | 
| 100 |     4 |    1 |     1 |      1 |     1 |   90 | 2015-03-10 | 
| 100 |     5 |    1 |     1 |      1 |     1 |  120 | 2015-03-10 | 
| 100 |     6 |    1 |     1 |      1 |     1 |  150 | 2015-03-10 | 
| 100 |     7 |    1 |     1 |      1 |     1 |   0 | 2015-03-11 | 
| 100 |     8 |    1 |     1 |      1 |     1 |   30 | 2015-03-11 | 
| 100 |     9 |    1 |     1 |      1 |     1 |   60 | 2015-03-11 | 
| 100 |    10 |    1 |     1 |      1 |     1 |   90 | 2015-03-11 | 
| 100 |    11 |    1 |     1 |      1 |     1 |  120 | 2015-03-11 | 
| 100 |    12 |    1 |     1 |      1 |     1 |  150 | 2015-03-11 | 
| 100 |    13 |    1 |     1 |      1 |     1 |   0 | 2015-03-12 | 
| 100 |    14 |    1 |     1 |      1 |     1 |   30 | 2015-03-12 | 
| 100 |    15 |    1 |     1 |      1 |     1 |   60 | 2015-03-12 | 
| 100 |    16 |    1 |     1 |      1 |     1 |   90 | 2015-03-12 | 
| 100 |    17 |    1 |     1 |      1 |     1 |  120 | 2015-03-12 | 
| 100 |    18 |    1 |     1 |      1 |     1 |  150 | 2015-03-12 | 
| 100 |    19 |    1 |     1 |      1 |     1 |   0 | 2015-03-13 | 
| 100 |    20 |    1 |     1 |      1 |     1 |   30 | 2015-03-13 | 
| 100 |    21 |    1 |     1 |      1 |     1 |   60 | 2015-03-13 | 
| 100 |    22 |    1 |     1 |      1 |     1 |   90 | 2015-03-13 | 
| 100 |    23 |    1 |     1 |      1 |     1 |  120 | 2015-03-13 | 
| 100 |    24 |    1 |     1 |      1 |     1 |  150 | 2015-03-13 | 
| 101 |    25 |    1 |     1 |      1 |     1 |   0 | 2015-03-10 | 
| 101 |    26 |    1 |     1 |      1 |     1 |   30 | 2015-03-10 | 
| 101 |    27 |    1 |     1 |      1 |     1 |   60 | 2015-03-10 | 
| 101 |    28 |    1 |     1 |      1 |     1 |   90 | 2015-03-10 | 
| 101 |    29 |    1 |     1 |      1 |     1 |  120 | 2015-03-10 | 
| 101 |    30 |    1 |     1 |      1 |     1 |  150 | 2015-03-10 | 
| 101 |    31 |    1 |     1 |      1 |     1 |   0 | 2015-03-11 | 
| 101 |    32 |    1 |     1 |      1 |     1 |   30 | 2015-03-11 | 
| 101 |    33 |    1 |     1 |      1 |     1 |   60 | 2015-03-11 | 
| 101 |    34 |    1 |     1 |      1 |     1 |   90 | 2015-03-11 | 
| 101 |    35 |    1 |     1 |      1 |     1 |  120 | 2015-03-11 | 
| 101 |    36 |    1 |     1 |      1 |     1 |  150 | 2015-03-11 | 
| 101 |    37 |    1 |     1 |      1 |     1 |   0 | 2015-03-12 | 
| 101 |    38 |    1 |     1 |      1 |     1 |   30 | 2015-03-12 | 
| 101 |    39 |    1 |     1 |      1 |     1 |   60 | 2015-03-12 | 
| 101 |    40 |    1 |     1 |      1 |     1 |   90 | 2015-03-12 | 
| 101 |    41 |    1 |     1 |      1 |     1 |  120 | 2015-03-12 | 
| 101 |    42 |    1 |     1 |      1 |     1 |  150 | 2015-03-12 | 
| 101 |    43 |    1 |     1 |      1 |     1 |   0 | 2015-03-13 | 
| 101 |    44 |    1 |     1 |      1 |     1 |   30 | 2015-03-13 | 
| 101 |    45 |    1 |     1 |      1 |     1 |   60 | 2015-03-13 | 
| 101 |    46 |    1 |     1 |      1 |     1 |   90 | 2015-03-13 | 
| 101 |    47 |    1 |     1 |      1 |     1 |  120 | 2015-03-13 | 
| 101 |    48 |    1 |     1 |      1 |     1 |  150 | 2015-03-13 | 
| 102 |    49 |    1 |     1 |      1 |     1 |   0 | 2015-03-10 | 
| 102 |    50 |    1 |     1 |      1 |     1 |   30 | 2015-03-10 | 
| 102 |    51 |    1 |     1 |      1 |     1 |   60 | 2015-03-10 | 
| 102 |    52 |    1 |     1 |      1 |     1 |   90 | 2015-03-10 | 
| 102 |    53 |    1 |     1 |      1 |     1 |  120 | 2015-03-10 | 
| 102 |    54 |    1 |     1 |      1 |     1 |  150 | 2015-03-10 | 
| 102 |    55 |    1 |     1 |      1 |     1 |   0 | 2015-03-11 | 
| 102 |    56 |    1 |     1 |      1 |     1 |   30 | 2015-03-11 | 
| 102 |    57 |    1 |     1 |      1 |     1 |   60 | 2015-03-11 | 
| 102 |    58 |    1 |     1 |      1 |     1 |   90 | 2015-03-11 | 
| 102 |    59 |    1 |     1 |      1 |     1 |  120 | 2015-03-11 | 
| 102 |    60 |    1 |     1 |      1 |     1 |  150 | 2015-03-11 | 
| 102 |    61 |    1 |     1 |      1 |     1 |   0 | 2015-03-12 | 
| 102 |    62 |    1 |     1 |      1 |     1 |   30 | 2015-03-12 | 
| 102 |    63 |    1 |     1 |      1 |     1 |   60 | 2015-03-12 | 
| 102 |    64 |    1 |     1 |      1 |     1 |   90 | 2015-03-12 | 
| 102 |    65 |    1 |     1 |      1 |     1 |  120 | 2015-03-12 | 
| 102 |    66 |    1 |     1 |      1 |     1 |  150 | 2015-03-12 | 
| 102 |    67 |    1 |     1 |      1 |     1 |   0 | 2015-03-13 | 
| 102 |    68 |    1 |     1 |      1 |     1 |   30 | 2015-03-13 | 
| 102 |    69 |    1 |     1 |      1 |     1 |   60 | 2015-03-13 | 
| 102 |    70 |    1 |     1 |      1 |     1 |   90 | 2015-03-13 | 
| 102 |    71 |    1 |     1 |      1 |     1 |  120 | 2015-03-13 | 
| 102 |    72 |    1 |     1 |      1 |     1 |  150 | 2015-03-13 | 
+--------+-------------------+-----------------+--------------------+------------------------+----------------------+------------+------------+ 

我想用這個公式先計算每個寵物的日分數:

score = SUM(severe_movement_time) + SUM(moderate_movement_time) + SUM(mild_movement_time) + SUM(simple_sleep_time), 

,然後再決定各寵物的等級基於它的分數。重要提示:寵物的一天活動數據會在兩天內擴展。例如,對於pet_id = 100,2015-03-11的滿足以下條件的活動數據:

(date = DATE_SUB(DATE('2015-03-11'), INTERVAL 1 DAY) AND start_time >= 120 AND start_time <= 150) OR (date = DATE('2015-03-11') AND start_time <= (120 - 30) AND start_time >= 0). 

即,2015-03-11的活動數據包含以下6個條目:

date=2015-03-10, start_time=120 
date=2015-03-10, start_time=150 
date=2015-03-11, start_time=0 
date=2015-03-11, start_time=30 
date=2015-03-11, start_time=60 
date=2015-03-11, start_time=90 

通過使用以下查詢,我可以做一天,2015-03-11。

SELECT pet_id, date, score, rank 
FROM 
(
    SELECT scoreFinder.pet_id, scoreFinder.date, scoreFinder.score, @prev := @curr, @curr := score, @rank := IF(@prev = @curr, @rank, @rank+1) AS rank 
    FROM 
     (
      SELECT pet_id, date, (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_SUB(DATE('2015-03-11'), INTERVAL 1 DAY) AND start_time >= 120 AND start_time <= 150) OR 
        (date = DATE('2015-03-11') AND start_time <= (120 - 30) AND start_time >= 0))  
      GROUP BY pet_id 
     ) scoreFinder, (SELECT @curr := null, @prev := null, @rank := 0) rank 
    ORDER BY score DESC 
) rankFinder 

我的問題是如何編寫一個查詢來計算連續幾天的分數和排名,例如,3天(起始= 2015-03-11日期,結束日期= 2015年3月13日) 。預期結果如下所示。

| pet_id | date  | score | rank | 
+--------+------------+-------+------+ 
| 102 | 2015-03-11 | 351 | 1 | 
| 101 | 2015-03-11 | 207 | 2 | 
| 100 | 2015-03-11 | 63 | 3 | 
| 102 | 2015-03-12 | 387 | 1 | 
| 101 | 2015-03-12 | 243 | 2 | 
| 100 | 2015-03-12 | 99 | 3 | 
| 102 | 2015-03-13 | 423 | 1 | 
| 101 | 2015-03-13 | 279 | 2 | 
| 100 | 2015-03-13 | 135 | 3 | 

回答

1

這裏是一個解決方案 - 這裏的基本思想是產生在子查詢的日期範圍,然後用TEST_ACTIVITY表加入吧:

SELECT scores.*, 
     @rank := IF(@prev = date, @rank + 1, 1) AS rank, 
     @prev := date 
FROM (SELECT pet_id, 
       dates.date, 
       (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 (SELECT DISTINCT date 
       FROM TEST_ACTIVITY 
       WHERE date >= '2015-03-11' 
         AND date <= '2015-03-13' 
       ORDER BY date) dates 
       JOIN TEST_ACTIVITY t 
       ON ((t.date = Date_sub(Date(dates.date), INTERVAL 1 day) 
         AND t.start_time >= 120 
         AND t.start_time <= 150) 
         OR (t.date = Date(dates.date) 
          AND t.start_time <= (120 - 30) 
          AND t.start_time >= 0)) 
     GROUP BY dates.date, 
        t.pet_id 
     ORDER BY dates.date, 
        score DESC) scores, 
     (SELECT @prev := NULL, 
       @rank := 0) rank; 

對您的測試數據結果:

pet_id date score rank @prev := date 
102 2015-03-11 351 1 2015-03-11 
101 2015-03-11 207 2 2015-03-11 
100 2015-03-11 63 3 2015-03-11 
102 2015-03-12 387 1 2015-03-12 
101 2015-03-12 243 2 2015-03-12 
100 2015-03-12 99 3 2015-03-12 
102 2015-03-13 423 1 2015-03-13 
101 2015-03-13 279 2 2015-03-13 
100 2015-03-13 135 3 2015-03-13 
+0

感謝您的解決方案 –