# SQL How to Sum SELECT

Wed Apr 01 2020 08:18:27 GMT+0000 (Coordinated Universal Time)

Saved by @DayAnn #sql #sum

```Use Sum aggregate function with case statement as below, if you want sum of those t.value based on t.clock then do group by on that column so that you will get clock while sum of values.

select sum(case when  i.hostid='223344'   and t.itemid = '0223344' then t.value end) as FirstValue,sum(case when  i.hostid='112233' and t.itemid = '0112233' then t.value end) as SecondValue
from hosts h, items i,history_uint t
where i.hostid=h.hostid and t.itemid=i.itemid
If it is based on t.clock then

select t.clock, sum(case when  i.hostid='223344'   and t.itemid = '0223344' then t.value end) as FirstValue,sum(case when  i.hostid='112233' and t.itemid = '0112233' then t.value end) as SecondValue
from hosts h, items i,history_uint t
where i.hostid=h.hostid and t.itemid=i.itemid
group by t.clock
0

You can try this below logic-

SELECT i.hostid,t.itemid,t.value as OneValues, t.clock as time
FROM hosts h
INNER JOIN items i
ON i.hostid=h.hostid
AND i.hostid IN ('223344','112233')
AND i.itemid IN ('0223344','0112233')
INNER JOIN  history_uint t
ON t.itemid=i.itemid```
content_copyCOPY

I need sum t.value from one select and t.value from other

https://stackoverflow.com/questions/60963447/sql-how-to-sum-select