【问题标题】:Vertica/vSQL " select 10 rows per day with order and on certain groupVertica/vSQL " 每天选择 10 行,按顺序和特定组
【发布时间】:2025-12-17 01:10:01
【问题描述】:

我有 Vertica 数据库,其中包含许多带有日期(时间戳)和更多属性的记录。例如,'testTable' 看起来像

a varchar(255)
b int
timestamp bigint

我需要在一段时间内(比如 1 月 1 日至 1 月 15 日)每天查找前 10 个 sum(b),这些日期可以由用户指定。

迭代查询会是什么样子?粗略的方法可能是单独的 SELECT 语句,UNION ALL 介于两者之间。

select a, sum(b) from testTable where TO_TIMESTAMP( 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 TO_TIMESTAMP( 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 TO_TIMESTAMP( 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 TO_TIMESTAMP( 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 ;

但我希望它更通用,用户可以运行具有两个给定日期的脚本。

【问题讨论】:

标签: vertica


【解决方案1】:

语法可能有点不对...我没有要测试的 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

【讨论】: