Let's say I have 2 tables:
table device
is a set of measuring devices, table data
is a set of values of different types measured by the devices.
Table data = (no, id,value_type,value, dayhour) no is PK, id refer to table device
Table device = (id, name) id is PK
I currently have a query that will obtain the sum of all values of a specific value_type
generated by an id
on a specific date, something like:
SELECT SUM(cast(a.value as int)),b.name FROM data a INNER JOIN device b
ON a.id=b.id
AND a.id=1
AND a.value_type=2
AND date(a.dayhour)='2015-12-12'
GROUP BY b.name
The query works without problems. Now I want to be able to subtract the sum of different value types. What I'm currently doing is two queries, one for obtaining the sum for value_type
2 and another for the sum for value_type
3 and then doing the subtraction at an upper layer process.
However, I would like to do this from a single query, something like a query that will retrieve one column with the sum of value_type
2, another with the sum of value_type
3 and a third one with the subtraction of these 2 columns.