2010-05-10 92 views
1

行的MySQL奇才疏MySQL表到多個行:由時間戳

我已經從定義爲多個探針位置數據的表如下:

 
+----------+----------+------+-----+---------+-------+ 
| Field | Type  | Null | Key | Default | Extra | 
+----------+----------+------+-----+---------+-------+ 
| time  | datetime | NO |  | NULL |  | 
| probe_id | char(3) | NO |  | NULL |  | 
| position | float | NO |  | NULL |  | 
+----------+----------+------+-----+---------+-------+ 

一個簡單的選擇輸出是這樣的:

 
+---------------------+----------+----------+ 
| time    | probe_id | position | 
+---------------------+----------+----------+ 
| 2010-05-05 14:16:42 | 00A  | 0.0045 | 
| 2010-05-05 14:16:42 | 00B  | 0.0005 | 
| 2010-05-05 14:16:42 | 00C  | 0.002 | 
| 2010-05-05 14:16:42 | 01A  |  0 | 
| 2010-05-05 14:16:42 | 01B  | 0.001 | 
| 2010-05-05 14:16:42 | 01C  | 0.0025 | 
| 2010-05-05 14:16:43 | 00A  | 0.0045 | 
| 2010-05-05 14:16:43 | 00B  | 0.0005 | 
| 2010-05-05 14:16:43 | 00C  | 0.002 | 
| 2010-05-05 14:16:43 | 01A  |  0 | 
|   .   | .  |  . | 
|   .   | .  |  . | 
|   .   | .  |  . | 
+---------------------+----------+----------+ 

不過,我想輸出是這樣的:

 
+---------------------+--------+--------+-------+-----+-------+--------+ 
| time    | 00A | 00B | 00C | 01A | 01B | 01C | 
+---------------------+--------+--------+-------+-----+-------+--------+ 
| 2010-05-05 14:16:42 | 0.0045 | 0.0005 | 0.002 | 0 | 0.001 | 0.0025 | 
| 2010-05-05 14:16:43 | 0.0045 | 0.0005 | 0.002 | 0 | 0.001 | 0.0025 | 
| 2010-05-05 14:16:44 | 0.0045 | 0.0005 | 0.002 | 0 | 0.001 | 0.0025 | 
| 2010-05-05 14:16:45 | 0.0045 | 0.0005 | 0.002 | 0 | 0.001 | 0.0025 | 
| 2010-05-05 14:16:46 | 0.0045 | 0.0005 | 0.002 | 0 | 0.001 | 0.0025 | 
| 2010-05-05 14:16:47 | 0.0045 | 0.0005 | 0.002 | 0 | 0.001 | 0.0025 | 
|   .   | . | . | . | . | . | . | 
|   .   | . | . | . | . | . | . | 
|   .   | . | . | . | . | . | . | 
+---------------------+--------+--------+-------+-----+-------+--------+ 

理想情況下,根據表中的數據動態生成不同的探針位置列。這是可能的,還是我把我的頭髮拉出來沒有?

我試着GROUP_CONCAT的GROUP BY時間大致獲取數據,但我無法將該輸出分隔到probe_id列中。

 
mysql> SELECT time, GROUP_CONCAT(probe_id), GROUP_CONCAT(position) FROM MG41 GROUP BY time LIMIT 10; 
+---------------------+-------------------------+------------------------------------+ 
| time    | GROUP_CONCAT(probe_id) | GROUP_CONCAT(position)    | 
+---------------------+-------------------------+------------------------------------+ 
| 2010-05-05 14:16:42 | 00A,00B,00C,01A,01B,01C | 0.0045,0.0005,0.002,0,0.001,0.0025 | 
| 2010-05-05 14:16:43 | 01C,01B,01A,00C,00B,00A | 0.0025,0.001,0,0.002,0.0005,0.0045 | 
| 2010-05-05 14:16:44 | 01C,01B,01A,00C,00B,00A | 0.0025,0.001,0,0.002,0.0005,0.0045 | 
| 2010-05-05 14:16:45 | 01C,01B,01A,00C,00B,00A | 0.0025,0.001,0,0.002,0.0005,0.0045 | 
| 2010-05-05 14:16:46 | 01C,01B,01A,00C,00B,00A | 0.0025,0.001,0,0.002,0.0005,0.0045 | 
| 2010-05-05 14:16:47 | 01C,01B,01A,00C,00B,00A | 0.0025,0.001,0,0.002,0.0005,0.0045 | 
| 2010-05-05 14:16:48 | 01C,01B,01A,00C,00B,00A | 0.0025,0.001,0,0.002,0.0005,0.0045 | 
| 2010-05-05 14:16:49 | 01C,01B,01A,00C,00B,00A | 0.0025,0.001,0,0.002,0.0005,0.0045 | 
| 2010-05-05 14:16:50 | 01C,01B,01A,00C,00B,00A | 0.0025,0.001,0,0.002,0.0005,0.0045 | 
| 2010-05-05 14:16:51 | 01C,01B,01A,00C,00B,00A | 0.0025,0.001,0,0.002,0.0005,0.0045 | 
+---------------------+-------------------------+------------------------------------+ 

回答

2
SELECT 
    time, 
    SUM(CASE WHEN probe_id = '00A' THEN position ELSE 0 END) `00A`, 
    SUM(CASE WHEN probe_id = '00B' THEN position ELSE 0 END) `00B`, 
    SUM(CASE WHEN probe_id = '00C' THEN position ELSE 0 END) `00C`, 
    SUM(CASE WHEN probe_id = '01A' THEN position ELSE 0 END) `01A`, 
    SUM(CASE WHEN probe_id = '01B' THEN position ELSE 0 END) `01B`, 
    SUM(CASE WHEN probe_id = '01C' THEN position ELSE 0 END) `01C` 
FROM MG41 
GROUP BY time LIMIT 10; 
+0

謝謝馬克。我添加了ROUND(x,4)來擺脫SUM的額外小數位。 – 2010-05-11 17:20:54

1

要有行的動態數量,您將需要一個動態生成的查詢。 (如果你事先知道probe_ids,那麼你可以JUSE使用靜態查詢。)

一般形式將

SELECT time, P00A, P00B, ... P03B, etc.. 
    FROM 
     (SELECT time FROM MG41 GROUP BY time) allTimes 
    INNER JOIN 
     (SELECT time, position AS P00A from MG41 WHERE probe_id='00A') pt00A 
     ON pt00A.time=allTimes.time 
    INNER JOIN 
     (SELECT time, position AS P00B from MG41 WHERE probe_id='00B') pt00B 
     ON pt00B.time=allTimes.time 

etc... 

然後您打造出來的內部連接和所有不同的選擇的行探頭。