2013-03-24 76 views
1

我很努力從數據收集環境的CSV風格數據的「規範化」數據庫中提取數據。這將使用PHP/MySQL/JSON完成,目的是繪製數據。我認爲我有過濾部分,但我需要幫助進行旋轉。這個想法是使用keyNames字段作爲數據的過濾器。使用鍵/值對過濾和分組來自表的數據

下面是查詢:

select d.testId,d.rowId,f.keyName,f.keyValue from tests t 
inner join data d on t.testId = d.testId 
inner join data c on t.testId = c.testId and c.rowId = d.rowId 
join data f on f.testId = t.testId and f.rowId = d.rowId 
where (d.keyName = 'voltage' and d.keyValue < 5) and (c.keyName = 'temperature' and c.keyValue = 30) and (t.testType = 'testType1'); 

和結果:

+--------+-------+-------------+----------+ 
| testId | rowId | keyName  | keyValue | 
+--------+-------+-------------+----------+ 
|  1 |  1 | voltage  |  4 | 
|  1 |  1 | temperature |  30 | 
|  1 |  1 | velocity |  20 | 
|  1 |  2 | voltage  |  4 | 
|  1 |  2 | temperature |  30 | 
|  1 |  2 | velocity |  21 | 
|  2 |  1 | voltage  |  4 | 
|  2 |  1 | temperature |  30 | 
|  2 |  1 | velocity |  30 | 
|  2 |  2 | voltage  |  4 | 
|  2 |  2 | temperature |  30 | 
|  2 |  2 | velocity |  31 | 
+--------+-------+-------------+----------+ 

我想樞轉進入該:testId,ROWID,電壓,溫度,速度,如:

+--------+-------+---------+-------------+----------+ 
| testId | rowId | voltage | temperature | velocity | 
+--------+-------+---------+-------------+----------+ 
|  1 |  1 |  4 |   30 |  20 | 
|  1 |  2 |  4 |   30 |  21 | 
|  2 |  1 |  4 |   30 |  30 | 
|  2 |  2 |  4 |   30 |  31 | 
+--------+-------+---------+-------------+----------+ 

任何想法?我覺得我接近這一點:

mysql> select f.testId,f.rowId,(if(f.keyName='voltage',f.keyValue,NULL)) as 'voltage',(if(f.keyName='temperature',f.keyValue,NULL)) as 'temperature',(if(f.keyName='velocity',f.keyValue,NULL)) as 'velocity' from tests t inner join data d on t.testId = d.testId inner join data c on t.testId = c.testId and c.rowId = d.rowId join data f on f.testId = t.testId and f.rowId = d.rowId where (d.keyName = 'voltage' and d.keyValue < 5) and (c.keyName = 'temperature' and c.keyValue = 30) and (t.testType = 'testType1'); 
+--------+-------+---------+-------------+----------+ 
| testId | rowId | voltage | temperature | velocity | 
+--------+-------+---------+-------------+----------+ 
|  1 |  1 |  4 |  NULL |  NULL | 
|  1 |  1 | NULL |   30 |  NULL | 
|  1 |  1 | NULL |  NULL |  20 | 
|  1 |  2 |  4 |  NULL |  NULL | 
|  1 |  2 | NULL |   30 |  NULL | 
|  1 |  2 | NULL |  NULL |  21 | 
|  2 |  1 |  4 |  NULL |  NULL | 
|  2 |  1 | NULL |   30 |  NULL | 
|  2 |  1 | NULL |  NULL |  30 | 
|  2 |  2 |  4 |  NULL |  NULL | 
|  2 |  2 | NULL |   30 |  NULL | 
|  2 |  2 | NULL |  NULL |  31 | 
+--------+-------+---------+-------------+----------+ 

下面是引用表的定義和數據源:

CREATE TABLE IF NOT EXISTS `data` (
    `ptId` int(11) NOT NULL AUTO_INCREMENT, 
    `testId` int(11) NOT NULL, 
    `rowId` int(11) NOT NULL, 
    `keyName` text NOT NULL, 
    `keyValue` int(11) NOT NULL, 
    PRIMARY KEY (`ptId`) 
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=33 ; 

INSERT INTO `data` (`ptId`, `testId`, `rowId`, `keyName`, `keyValue`) VALUES 
(1, 1, 1, 'voltage', 4), 
(2, 1, 1, 'temperature', 30), 
(3, 1, 1, 'velocity', 20), 
(4, 1, 2, 'voltage', 4), 
(5, 1, 2, 'temperature', 30), 
(6, 1, 2, 'velocity', 21), 
(7, 1, 3, 'voltage', 3), 
(8, 1, 3, 'temperature', 35), 
(9, 1, 3, 'velocity', 22), 
(10, 1, 4, 'voltage', 3), 
(11, 1, 4, 'temperature', 35), 
(12, 1, 4, 'velocity', 23), 
(13, 2, 1, 'voltage', 4), 
(14, 2, 1, 'temperature', 30), 
(15, 2, 1, 'velocity', 30), 
(16, 2, 2, 'voltage', 4), 
(17, 2, 2, 'temperature', 30), 
(18, 2, 2, 'velocity', 31), 
(19, 2, 3, 'voltage', 5), 
(20, 2, 3, 'temperature', 35), 
(21, 2, 3, 'velocity', 32), 
(22, 2, 4, 'voltage', 5), 
(23, 2, 4, 'temperature', 35), 
(24, 2, 4, 'velocity', 33), 
(25, 4, 1, 'voltage', 4), 
(26, 4, 1, 'velocity', 30), 
(27, 4, 2, 'voltage', 4), 
(28, 4, 2, 'velocity', 31), 
(29, 4, 3, 'voltage', 5), 
(30, 4, 3, 'velocity', 32), 
(31, 4, 4, 'voltage', 5), 
(32, 4, 4, 'velocity', 33); 

CREATE TABLE IF NOT EXISTS `tests` (
    `testId` int(11) NOT NULL AUTO_INCREMENT COMMENT 'Unique Test ID', 
    `testType` text NOT NULL, 
    `startDate` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, 
    `userName` text NOT NULL, 
    `testSoftware` text NOT NULL, 
    `comments` text, 
    `dutID` text, 
    PRIMARY KEY (`testId`) 
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=8 ; 

INSERT INTO `tests` (`testId`, `testType`, `startDate`, `userName`, `testSoftware`, `comments`, `dutID`) VALUES 
(1, 'testType1', '2013-03-23 21:06:10', 'testUser1', 'testSoftware1', NULL, 'dut1'), 
(2, 'testType1', '2013-03-23 21:10:32', 'testUser1', 'testSoftware1', NULL, 'dut1'), 
(3, 'testType1', '2013-03-23 21:10:32', 'testUser1', 'testSoftware1', NULL, 'dut1'), 
(4, 'testType2', '2013-03-23 21:10:32', 'testUser1', 'testSoftware1', NULL, 'dut1'); 
+1

您付多少錢? – Jocelyn 2013-03-24 01:02:35

+0

這不適用於付費應用程序。該評論如何有用? – jdhar 2013-03-24 02:55:13

+1

這裏似乎沒有問題,只是你想要達到的目標列表。請問一個具體的問題,我相信你會得到一個很好的,有用的答案。 – vascowhite 2013-03-24 19:47:19

回答

1

你的問題的部分原因是你在SELECT列表中使用聚合函數但你沒有使用GROUP BY。您應該使用一個GROUP BY與此類似:

GROUP BY d.testId, d.rowId 

每當你使用聚合函數,你必須在你的選擇等欄目,他們應該是一組分析研究。因此,您的完整查詢應該是:

select d.testId, 
    d.rowId, 
    max(if(f.keyName='voltage',f.keyValue,NULL)) as 'voltage', 
    max(if(f.keyName='temperature',f.keyValue,NULL)) as 'temperature', 
    max(if(f.keyName='velocity',f.keyValue,NULL)) as 'velocity' 
from tests t 
inner join data d 
    on t.testId = d.testId 
inner join data c 
    on t.testId = c.testId 
    and c.rowId = d.rowId 
join data f 
    on f.testId = t.testId 
    and f.rowId = d.rowId 
where (d.keyName = 'voltage' and d.keyValue < 5) 
    and (c.keyName = 'temperature' and c.keyValue = 30) 
    and (t.testType = 'testType1') 
GROUP BY d.testId, d.rowId 

請注意,雖然您的實際數據結構未顯示在原始問題中。看起來,這可以合併到以下內容:

select d.testid, 
    d.rowid, 
    max(case when d.keyName = 'voltage' and d.keyValue < 5 then d.keyValue end) voltage, 
    max(case when d.keyName = 'temperature' and d.keyValue =30 then d.keyValue end) temperature, 
    max(case when d.keyName = 'velocity' then d.keyValue end) velocity 
from tests t 
left join data d 
    on t.testid = d.testid 
group by d.testid, d.rowid 

請參閱SQL Fiddle with Demo。這給出了data表中只有一個連接的結果:

| TESTID | ROWID | VOLTAGE | TEMPERATURE | VELOCITY | 
----------------------------------------------------- 
|  1 |  1 |  4 |   30 |  20 | 
|  1 |  2 |  4 |   30 |  21 | 
|  2 |  1 |  4 |   30 |  30 | 
|  2 |  2 |  4 |   30 |  31 | 
+0

非常有幫助,謝謝!看起來像group by是失蹤的關鍵。我試過你的優化,我不認爲它執行邏輯和我需要。我正在嘗試您的原始解決方案,看看我是否正確理解它,並且它適合我的需求。我已更新原始文章以包含數據結構和樣本數據。 – jdhar 2013-03-25 00:47:18

+0

@jdhar根據您提供的樣本數據,您希望得到的結果是什麼? – Taryn 2013-03-25 01:31:31

+0

我已經包含了您的第一個查詢獲得的所需輸出。我們的想法是使用keyName與keyField比較的任意組合來過濾數據(我給出的例子是溫度= 30,電壓<5)。所以它看起來像添加字段,我只是複製內部聯接塊。 – jdhar 2013-03-25 01:55:12

相關問題