我們使用監視解決方案(zabbix)。它存儲使用稱爲trends_uint具有以下結構(簡化的)表趨勢數據(MySQL的5.X):使用Unix時間SQL查詢 - 替換硬編碼值...(MySQL)
itemid clock value_avg
1 1238774400 100
1 1250773900 70
1 1250773200 50
時鐘字段存儲日期時間值。
我試圖提取報告來查看受監視項目的消耗。我認爲通過選擇最舊的記錄和最新的記錄來做到這一點。我做到了這一點:
SELECT
(SELECT t.clock FROM trends_uint t WHERE t.itemid = 1 AND t.clock IN (SELECT MIN(t.clock) FROM trends_uint t WHERE t.itemid = 1)) AS minClock,
(SELECT t.value_avg FROM trends_uint t WHERE t.itemid = 1 AND t.clock IN (SELECT MIN(t.clock) FROM trends_uint t WHERE t.itemid = 1)) AS minValueAvg,
(SELECT t.clock FROM trends_uint t WHERE t.itemid = 1 AND t.clock IN (SELECT MAX(t.clock) FROM trends_uint t WHERE t.itemid = 1)) AS maxClock,
(SELECT t.value_avg FROM trends_uint t WHERE t.itemid = 1 AND t.clock IN (SELECT MAX(t.clock) FROM trends_uint t WHERE t.itemid = 1)) AS maxValueAvg,
(SELECT maxClock - minClock) AS timeGap,
(SELECT minValueAvg - maxValueAvg) AS valueGap,
(SELECT timeGap/86400) AS daysGap,
(SELECT valueGap/daysGap/1024/1024) AS consumeMB
我的問題是,我有很多的服務器,我想從中計算消費。我不想改變查詢哪一個(改變itemid)。
該系統有另一個表格(項目),我可以從中提取需要放入報表的項目。像SELECT itemid FROM items WHERE ...
東西,所以這是我的問題:而不是使用硬編碼的itemid,是有可能使其「動態」的,例如使用返回代碼從另一個查詢(例如:SELECT ... FROM ... WHERE itemid IN (SELECT itemid FROM items WHERE ...
)?
TIA,
鮑勃
感謝。我沒」但我試圖用MySQL功能來實現它(如果可能的話,優先選擇純SQL查詢),我將對用戶定義的變量進行研究。 – 2009-08-20 16:34:42