我有 Vertica 数据库,其中包含许多带有日期(时间戳)和更多属性的记录。例如,“testTable”看起来像
a varchar(255)
b int
timestamp bigint
我需要在sum(b)
一段时间内(比如 1 月 1 日到 1 月 15 日)找到每天的前 10 个日期,这些日期可以由用户指定。
迭代查询会是什么样子?粗略的方式可能是介于两者之间的个人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 ;
但我希望它更通用,用户可以运行具有两个给定日期的脚本。