Hi,
I've got 27 tables all in the format edate(datetime),in(bigint),out(bigint). I want to achive a select that in a specified period for each edate value it will give me the sum of `in` and `out` across all of the tables.
I have used this query (with all of the tables in) that is generated through php doing a select to a lookup table to find the current tables to select from.
which returns the right result as long as all of the tables have data in for that datetime. The problem is that there will be more tables added in the future so there will not always be data from each table for a date.
I don't have control over the tables so I cannot change the format, and this query was getting a bit big. Can anyone think of a better solution?
Thanks
Barney
I've got 27 tables all in the format edate(datetime),in(bigint),out(bigint). I want to achive a select that in a specified period for each edate value it will give me the sum of `in` and `out` across all of the tables.
I have used this query (with all of the tables in) that is generated through php doing a select to a lookup table to find the current tables to select from.
Code:
SELECT t1.edate AS slot,
( SUM(t1.in) + SUM(t2.in) + SUM(t3.in) + SUM(t4.in) + SUM(t5.in) + SUM(t6.in)) AS sum_in,
( SUM(t1.out) + SUM(t2.out) + SUM(t3.out) + SUM(t4.out) + SUM(t5.out) + SUM(t6.out)) AS sum_out
FROM `set0001_bw` AS t1
LEFT JOIN `set0002_bw` AS t2 USING(`timestamp`)
LEFT JOIN `set0003_bw` AS t3 USING(`timestamp`)
LEFT JOIN `set0004_bw` AS t4 USING(`timestamp`)
LEFT JOIN `set0005_bw` AS t5 USING(`timestamp`)
LEFT JOIN `set0006_bw` AS t6 USING(`timestamp`)
WHERE (t1.edate >= '2003-10-18' AND t1.edate < '2003-11-04')
OR (t2.edate >= '2003-10-18' AND t2.edate < '2003-11-04')
OR (t3.edate >= '2003-10-18' AND t3.edate < '2003-11-04')
OR (t4.edate >= '2003-10-18' AND t4.edate < '2003-11-04')
OR (t5.edate >= '2003-10-18' AND t5.edate < '2003-11-04')
OR (t6.edate >= '2003-10-18' AND t6.edate < '2003-11-04')
GROUP BY t1.edate;
I don't have control over the tables so I cannot change the format, and this query was getting a bit big. Can anyone think of a better solution?
Thanks
Barney