简体   繁体   中英

MySQL : select 10 rows per day with order and on certain group

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

Try this... Change the dates in the between range to pass the range only once.

Corrected for typo, omitted line, and missing comma:

select day, a, tot
from 
   (
   select 
      *,
      @num := if(@day =  tt4.day, @num + 1, 1) as row_number,
      @day := tt4.day as dummy
   from
      (
      select
         ts as day, 
         tt1.a, 
         sum(tt1.b) as tot
      from 
         testTable tt1, 
         ( select distinct date(FROM_UNIXTIME(tt2.timestamp)) as ts
           from   testTable tt2
           where  date(FROM_UNIXTIME(tt2.timestamp)) between cast('2012/01/01' as date) and cast('2012/01/15' as date) ) as tt3
      where 
         date(FROM_UNIXTIME(tt1.timestamp)) = tt3.ts
      group by 
         date(FROM_UNIXTIME(tt1.timestamp)), 
         tt1.a
      order by 
         date(FROM_UNIXTIME(tt1.timestamp)),
         sum(tt1.b) desc,
         tt1.a
      ) as tt4
   ) as tt5
where 
   tt5.row_number <=10

Modified - flavor of SQL changed for Vertica... syntax may be off (I don't have a Vertica installation to test against), but the gist is there.

select day, a, tot
from 
   (
   select 
      *,
      ROW_NUMBER() OVER (PARTITION BY tt4.day) as row_number
   from
      (
      select
         ts as day, 
         tt1.a, 
         sum(tt1.b) as tot
      from 
         testTable tt1, 
         ( select distinct date(TO_TIMESTAMP(tt2.timestamp)) as ts
           from   testTable tt2
           where  date(TO_TIMESTAMP(tt2.timestamp)) between cast('2012/01/01' as date) and cast('2012/01/15' as date) ) as tt3
      where 
         date(TO_TIMESTAMP(tt1.timestamp)) = tt3.ts
      group by 
         date(TO_TIMESTAMP(tt1.timestamp)), 
         tt1.a
      order by 
         date(TO_TIMESTAMP(tt1.timestamp)),
         sum(tt1.b) desc,
         tt1.a
      ) as tt4
   ) as tt5
where 
   tt5.row_number <=10

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM