SELECT year
, month
, day
, sum(tb1) as tb1
, sum(tb2) as tb2
, sum(tb3) as tb3
, (sum(tb1) + sum(tb2) + sum(tb3)) as total
FROM (
SELECT 'tb1' as tp,date,
strftime('%Y', date / 1000, 'unixepoch') as year,
strftime('%m', date / 1000, 'unixepoch') as month ,
strftime('%d', date / 1000, 'unixepoch') as day ,
value AS tb1,
0 tb2,
0 tb3,
FROM tb1
GROUP BY year,month,day
UNION
SELECT 'tb2' as tp,date,
strftime('%Y', date / 1000, 'unixepoch') as year,
strftime('%m', date / 1000, 'unixepoch') as month ,
strftime('%d', date / 1000, 'unixepoch') as day ,
0 tb1,
SUM( value ) AS tb2
0 tb3
FROM tb2
UNION
SELECT 'tb3' as tp,date,
strftime('%Y', date / 1000, 'unixepoch') as year,
strftime('%m', date / 1000, 'unixepoch') as month ,
strftime('%d', date / 1000, 'unixepoch') as day ,
0 tb1,
0 tb2,
value AS tb3
FROM tb3
) as x
GROUP BY year,month,day
ORDER BY year month, day