0

日付 (タイムスタンプ) を持つ多数のレコードとその他のいくつかの属性を持つ MySql データベースがあります。例として「testTable」は次のようになります


a varchar(255)
b int( 11)
タイムスタンプ bigint(20)

ユーザーが日付を指定できる 1 月 1 日から 1 月 15 日までの一定期間、日ごとに sum(b) の上位 10 件を見つける必要があります。

反復クエリはどのようになりますか? 大雑把な方法は、UNION ALLを間に挟んだ個々のselectステートメントである可能性があります。

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>

しかし、ユーザーが2つの特定の日付でスクリプトを実行できる、より一般的なものにしたいと考えています。

出力は
| のようなものです FROM_UNIXTIME(タイムスタンプ) | 合計 (b)
-----------+------------------------+------
テスト | 2012-01-01 03:24:41-04 | 500
テスト | 2012-01-01 03:19:40-04 | 420
テスト | 2012-01-01 03:14:39-04 | 261
テスト | 2012-01-01 03:09:38-04 | 244
テスト | 2012-01-01 03:04:37-04 | 231
テスト | 2012-01-01 02:59:36-04 | 223
テスト | 2012-01-01 02:54:35-04 | 211
テスト 1 | 2012-01-01 02:49:34-04 | 199
テスト 1 | 2012-01-01 03:24:41-04 | 195
テスト 1 | 2012-01-01 03:19:40-04 | 191
新しい | 2012-01-02 06:11:06-04 | 1000
新規 | 2012-01-02 06:06:06-04 | 978
新しい | 2012-01-02 06:01:06-04 | 867
新しい | 2012-01-02 05:56:05-04 | 786
新しい | 2012-01-02 05:51:05-04 | 698
新しい | 2012-01-02 05:46:05-04 | 598
新しい1 | 2012-01-02 06:11:06-04 | 476
新しい1 | 2012-01-02 05:41:04-04 | 345
新しい 2 | 2012-01-02 06:06:06-04 | 250
新しい2 | 2012-01-02 06:01:06-04 | 125

4

1 に答える 1

1

これを試してください...範囲を1回だけ渡すように、範囲間の日付を変更してください。

タイプミス、行の省略、カンマの欠落を修正しました。

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

変更-Vertica用にSQLのフレーバーが変更されました...構文がオフになっている可能性があります(テストするVerticaインストールがありません)が、要点はあります。

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
于 2012-07-16T19:15:54.310 に答える