SQL суммировать данные из двух таблиц
Подскажите, пожалуйста, можно ли получить сумму SUM(t.value) по этим данным из базы заббикса? Это айтемы с разных устройств, которые я опрашиваю с одинаковым промежутком, в графане пытаюсь построить суммированный график, один, вместо двух. Данные t.value - просто целые значения. Можно ли сделать из двух запросов один?
select t.value as Данные1, t.clock as time
from hosts h, items i,history_uint t
where i.hostid=h.hostid and t.itemid=i.itemid and i.hostid='112233' and t.itemid = '0112233'
select t.value as Данные2, t.clock as time
from hosts h, items i, history_uint t
where i.hostid=h.hostid and t.itemid=i.itemid and i.hostid='223344' and t.itemid = '0223344'
Ответы (1 шт):
Автор решения: Alex R.
→ Ссылка
Если просто объединить, то
select t.value as Данные, t.clock as time
from hosts h, items i,history_uint t
where i.hostid=h.hostid and t.itemid=i.itemid and i.hostid='112233' and t.itemid = '0112233'
union all
select t.value as Данные, t.clock as time
from hosts h, items i, history_uint t
where i.hostid=h.hostid and t.itemid=i.itemid and i.hostid='223344' and t.itemid = '0223344'
Если все таки требуется сумма, например Данные2 это количество, которое можно сложить, то
select
sum(g.data) as Данные, g.time
from (
select t.value as data, t.clock as time
from hosts h, items i,history_uint t
where i.hostid=h.hostid and t.itemid=i.itemid and i.hostid='112233' and t.itemid = '0112233'
union all
elect t.value as data, t.clock as time
from hosts h, items i, history_uint t
where i.hostid=h.hostid and t.itemid=i.itemid and i.hostid='223344' and t.itemid = '0223344') g
group by g.time
В этом случае в обоих запросах должны встерчаться строки с одинковым временем, иначе результат будет как в первом варианте.
Для справки: в инструкции UNION ALL имена полей называются по первому запросу.