查找每日数据SQL的每周峰值

时间:2016-03-08 19:56:16

标签: sql vertica

我将数据存储在Vertica sql数据库中,结构如下:

location date count
Seattle     2015-09-21  48991
Portland    2015-09-21  38396
Seattle     2015-09-22  49639
Portland    2015-09-22  28817
Portland    2015-09-23  29183
Seattle     2015-09-23  50210
Portland    2015-09-24  29627
Seattle     2015-09-24  50844
Seattle     2015-09-25  56485
Portland    2015-09-25  30076
Portland    2015-09-26  30352
Seattle     2015-09-26  52011
Portland    2015-09-27  30491
Seattle     2015-09-27  52291

计算是每日高峰。我想提取每周的最大数量,所以我基本上以每周峰值结束。

location week_ending_date count
Portland    2015-09-27  38396
Seattle     2015-09-27  56485   

有什么建议吗?

谢谢!

1 个答案:

答案 0 :(得分:1)

假设你的星期一开始......

SQL> select * from wtest order by dt ;
 location |     dt     | count 
----------+------------+-------
 Seattle  | 2015-09-21 | 48991
 Portland | 2015-09-21 | 38396
 Portland | 2015-09-22 | 28817
 Seattle  | 2015-09-22 | 49639
 Portland | 2015-09-23 | 29183
 Seattle  | 2015-09-23 | 50210
 Portland | 2015-09-24 | 29627
 Seattle  | 2015-09-24 | 50844
 Portland | 2015-09-25 | 30076
 Seattle  | 2015-09-25 | 56485
 Portland | 2015-09-26 | 30352
 Seattle  | 2015-09-26 | 52011
 Portland | 2015-09-27 | 30491
 Seattle  | 2015-09-27 | 52291

SQL> select 
        location, 
        min(dt) + 6 as week_ending_date, 
        max(count) as count 
     from 
        wtest 
     group by location, year_iso(dt), week_iso(dt) ;

 location | week_ending_date | count 
----------+------------------+-------
 Portland | 2015-09-27       | 38396
 Seattle  | 2015-09-27       | 56485