Reputation: 1071
This code gives me a nice result. But I want to add in some fields from another table, I'm not sure where to put the additioanal WHERE code...
select day, sum(diff) as total_diff
from (
select sub_meterID, date(`date`) as day, max(value) - min(value) as diff
from `sub_meter_data`
where date(`date`) > '2012-10-01'
and sub_meterID in ('58984','58985','58986','58987')
group by sub_meterID, date(`date`)
) a
group by day
The additional fields I need to match up are:
othertable.meterID = sub_meter_data.sub_meterID
moretable.meterID = othertableID
Upvotes: 0
Views: 153
Reputation: 1830
select day, sum(diff) as total_diff
from (
select t1.sub_meterID, date(t1.`date`) as day, max(t1.value) - min(t1.value) as diff
from `sub_meter_data` t1
JOIN othertable t2 ON t1.sub_meterID=t2.meterID
JOIN moretable t3 ON t2.ID=t3.meterID
where date(t1.`date`) > '2012-10-01'
and t1.sub_meterID in ('58984','58985','58986','58987')
group by t1.sub_meterID, date(t1.`date`)
) a
group by day
Read more info about MySQL Joins here
Upvotes: 1