我正在尝试将多个字段聚合到一个查询中,以获取相关历史记录表中每个项目的最新时钟/时间戳值。 这是用于墙面仪表板的显示,并且内置功能不足以获得整洁的输出,但它确实支持sql查询。
基于上一个问题(MySql Combine two queries (subselect or join or union)),我创建了以下内容,尽管它可以正常工作,但数据不正确。我需要从时钟列中提取每个项目的最后一个值。 一旦我们完成了表分区以及其他项目,将添加第三个表,这只是一个最小的例子。
SELECT hosts.host,
max(case when items.name='DB2 CPU Usage' then history.value end) as ' DB2 CPU Usage',
max(case when items.name='DB2 Cache Hit Rate' then history.value end) as ' DB2 Cache Hit Rate',
max(case when items.name='DB2 Percent Rqst Waiting' then history.value end) as ' DB2 Percent Rqst Waiting',
max(case when items.name='DB2 Version' then history_text.value end) as ' DB2 Version'
from hosts
left join items on items.hostid = hosts.hostid
left join history on history.itemid = items.itemid
left join history_text on history_text.itemid = items.itemid
where items.name like 'DB2%'
group by hosts.host, items.name, history.value
我尝试处理一些示例(例如Join tables and return row with latest timestamp和Retrieve latest timestamp row from table using INNER JOIN),但是无法使用所有表进行有效的查询。
这很好,但是我不知道如何组合
select itemid,max(clock) as max_clock from history group by history.itemid order by max_clock desc limit 1
以下是一些示例数据
hosts
+-----------+-------+
|host |hostid |
+-----------+-------+
|server01 |10001 |
|server02 |10002 |
+-----------+-------+
items
+-------+-------+-------------------+
|itemid |hostid |name |
+-------+-------+-------------------+
|9801 |10001 |DB2 CPU Usage |
|9936 |10001 |DB2 Cache Hit Rate |
|9783 |10001 |DB2 Version |
|9802 |10002 |DB2 CPU Usage |
|9937 |10002 |DB2 Cache Hit Rate |
|9784 |10002 |DB2 Version |
+-------+-------+-------------------+
history
+-------+-------+-----------+
|itemid |value |clock |
+-------+-------+-----------+
|9801 |86 |1565042153 |
|9801 |32 |1565042253 |
|9936 |12 |1565042120 |
|9936 |22 |1565042220 |
|9802 |41 |1565042153 |
|9802 |72 |1565042253 |
|9937 |99 |1565042120 |
|9937 |53 |1565042220 |
+-------+-------+-----------+
history_text
+-------+-------+-----------+
|itemid |value |clock |
|9783 |9.7 |1565042120 |
|9783 |9.7 |1565042320 |
|9784 |10.5 |1565042123 |
|9784 |10.5 |1565042324 |
+-------+-------+-----------+
和必需的输出
+-----------+---------------+-------------------+---------------+
| |DB2 CPU Usage |DB2 Cache Hit Rate |DB2 Version |
+-----------+---------------+-------------------+---------------+
|server01 |32 |22 |9.7 |
|server02 |72 |53 |10.5 |
+-----------+---------------+-------------------+---------------+
从我学到的每件事中,我已经适应了不同的查询-非常感谢您的宝贵时间!
答案 0 :(得分:0)
当您有两个历史记录表,一个具有第一个值,第二个具有第二个后继值时,您的数据库设计会更好。 首先,这是数据的创建版本,因此您可以使用dbfidddle或sqlfiddle快速尝试
CREATE TABLE hosts
(`host` varchar(8), `hostid` int)
;
INSERT INTO hosts
(`host`, `hostid`)
VALUES
('server01', 10001),
('server02', 10002)
;
CREATE TABLE items
(`itemid` int, `hostid` int, `name` varchar(18))
;
INSERT INTO items
(`itemid`, `hostid`, `name`)
VALUES
(9801, 10001, 'DB2 CPU Usage'),
(9936, 10001, 'DB2 Cache Hit Rate'),
(9783, 10001, 'DB2 Version'),
(9802, 10002, 'DB2 CPU Usage'),
(9937, 10002, 'DB2 Cache Hit Rate'),
(9784, 10002, 'DB2 Version')
;
CREATE TABLE history
(`itemid` int, `value` DECIMAL(5,1) , `clock` int)
;
INSERT INTO history
(`itemid`, `value`, `clock`)
VALUES
(9801, 86, 1565042153),
(9801, 32, 1565042253),
(9936, 12, 1565042120),
(9936, 22, 1565042220),
(9802, 41, 1565042153),
(9802, 72, 1565042253),
(9937, 99, 1565042120),
(9937, 53, 1565042220)
;
CREATE TABLE history_text
(`itemid` int, `value` DECIMAL(5,1) , `clock` int)
;
INSERT INTO history_text
(`itemid`, `value`, `clock`)
VALUES
(9783, 9.7, 1565042120),
(9783, 9.7, 1565042320),
(9784, 10.5, 1565042123),
(9784, 10.5, 1565042324)
;
这是我为您解决的问题
SELECT
host,
MAX(IF(name = 'DB2 CPU Usage',value,NULL)) 'DB2 CPU Usage',
MAX(IF(name = 'DB2 Cache Hit Rate',value,NULL)) 'DB2 Cache Hit Rate',
MAX(IF(name = 'DB2 Version',value,NULL)) 'DB2 Version'
FROM
(
SELECT i.itemid itemid, h.hostid hostid, name, value, h.host host FROM
(
SELECT MAX(value) value,h.itemid itemid FROM history h
INNER JOIN
(SELECT MAX(clock) clock,itemid FROM history
GROUP BY itemid
) hi
ON h.clock = hi.clock GROUP BY h.itemid
UNION
SELECT MAX(value) value,h.itemid itemid FROM history_text h
INNER JOIN
(SELECT MAX(clock) clock,itemid FROM history_text
GROUP BY itemid
) hi
ON h.clock = hi.clock GROUP BY h.itemid
) his
inner join items i on i.itemid = his.itemid
inner join hosts h on h.hostid = i.hostid
) res GROUP BY host;
结果是
host DB2 CPU Usage DB2 Cache Hit Rate DB2 Version
server01 32.0 22.0 9.7
server02 72.0 53.0 10.5
如您所见,我从历史和历史文本开始。 而且我找不到更漂亮的表格。 首先选择由itemid分组的最大日期,然后将其与相同的表连接起来并不复杂。则您为每个itemid都具有正确的值。 然后我对历史记录文本也做了同样的处理。 合并所有这些,因此我们拥有所有 是必需的。 其余的是表的其余部分内部的joinig。 最后得出结果