I have MySql database with lots of records with date (timestamp) and few more attributes. As an example 'testTable' looks like
a varchar(255)
b int( 11)
timestamp bigint(20)
I need to find top 10 of sum(b) for each day for a period of time say Jan 1st to Jan 15th where those dates can be specified by user.
How will the iterative query look like ? Crude way could be individual select statements with UNION ALL in between.
select a, sum(b) from testTable where FROM_UNIXTIME( timestamp ) between '2012-01-01 05:10:00' and '2012-01-02 05:10:00' group by a order by sum(b) desc LIMIT 10
UNION ALL
select a, sum(b) from testTable where FROM_UNIXTIME( timestamp ) between '2012-01-02 05:10:00' and '2012-01-03 05:10:00' group by a order by sum(b) desc LIMIT 10
UNION ALL
select a, sum(b) from testTable where FROM_UNIXTIME( timestamp ) between '2012-01-03 05:10:00' and '2012-01-04 05:10:00' group by a order by sum(b) desc LIMIT 10
..
..
..
UNION ALL
select a, sum(b) from testTable where FROM_UNIXTIME( timestamp ) between '2012-01-14 05:10:00' and '2012-01-15 05:10:00' group by a order by sum(b) desc LIMIT 10 ;</br>
But I want it to be more generic where user can run a script with 2 given dates.
output is like
a | FROM_UNIXTIME(timestamp) | sum (b)
-----------+------------------------+------
test | 2012-01-01 03:24:41-04 | 500
test | 2012-01-01 03:19:40-04 | 420
test | 2012-01-01 03:14:39-04 | 261
test | 2012-01-01 03:09:38-04 | 244
test | 2012-01-01 03:04:37-04 | 231
test | 2012-01-01 02:59:36-04 | 223
test | 2012-01-01 02:54:35-04 | 211
test1 | 2012-01-01 02:49:34-04 | 199
test1 | 2012-01-01 03:24:41-04 | 195
test1 | 2012-01-01 03:19:40-04 | 191
new | 2012-01-02 06:11:06-04 | 1000
new | 2012-01-02 06:06:06-04 | 978
new | 2012-01-02 06:01:06-04 | 867
new | 2012-01-02 05:56:05-04 | 786
new | 2012-01-02 05:51:05-04 | 698
new | 2012-01-02 05:46:05-04 | 598
new1 | 2012-01-02 06:11:06-04 | 476
new1 | 2012-01-02 05:41:04-04 | 345
new2 | 2012-01-02 06:06:06-04 | 250
new2 | 2012-01-02 06:01:06-04 | 125