如何通过具有最大的时间戳来获得组

时间:2019-01-08 13:12:03

标签: sql apache-drill

下面是具有来自HBASE的所有列的钻取查询结果。

+----------------+---------+---------+------------+------------+--------------+---------+---------+---------+---------+----------+---------------------------+----------------+
|     basicid    | column1 | column2 |column3|column4|   column5    |column6  | column7 | column8 | uniqueid| column10 |column11|    timestamp     |
+----------------+---------+---------+------------+------------+--------------+---------+---------+---------+---------+----------+---------------------------+----------------+
| basic_id/4657  | 4       | 408     | Test  | Test  | Centimeters  | length  | 50.0    | 60.0    | 13445   | 5271     | Test   | 1546938660000    |
| basic_id/4658  | 4       | 408     | Test  | Test  | Centimeters  | length  | 50.0    | 60.0    | 13445   | 5271     | Test   | 1546938720000    |
| basic_id/4659  | 4       | 408     | Test  | Test  | Centimeters  | length  | 50.0    | 60.0    | 13445   | 5271     | Test   | 1546938720000    |
| basic_id/4660  | 4       | 408     | Test  | Test  | Centimeters  | length  | 50.0    | 60.0    | 13446   | 5271     | Test   | 1546944120000    |
| basic_id/4661  | 4       | 408     | Test  | Test  | Centimeters  | length  | 50.0    | 60.0    | 13446   | 5271     | Test   | 1546944120000    |
+----------------+---------+---------+------------+------------+--------------+---------+---------+---------+---------+----------+---------------------------+----------------+

我希望具有最大时间戳的独特ID的唯一列元素(如果相同,也应该是一条记录)。

因此,结果应从以上具有最大时间戳的结果集中给出两个不同的唯一ID。 例如:

   +----------------+---------+---------+------------+------------+--------------+---------+---------+---------+---------+----------+---------------------------+----------------+
    |     basicid    | column1 | column2 |column3|column4|   column5    |column6  | column7 | column8 | uniqueid| column10 |column11|    timestamp     |
    +----------------+---------+---------+------------+------------+--------------+---------+---------+---------+---------+----------+---------------------------+----------------+
    | basic_id/4659  | 4       | 408     | Test  | Test  | Centimeters  | length  | 50.0    | 60.0    | 13445   | 5271     | Test   | 1546938720000    |
    | basic_id/4661  | 4       | 408     | Test  | Test  | Centimeters  | length  | 50.0    | 60.0    | 13446   | 5271     | Test   | 1546944120000    |
    +----------------+---------+---------+------------+------------+--------------+---------+---------+---------+---------+----------+---------------------------+----------------+

我正在尝试使用max函数进行时间戳记,但是在分组时我应该给所有列。如果我给出以上所有答复,我将不会得到。

2 个答案:

答案 0 :(得分:1)

从mytable组中按uniqueid选择uniqueid,max(timestamp)

答案 1 :(得分:0)

select * from (
select rank over(partition by uniqueid order by timestamp desc) as rnk
       ,uniqueid
       ,timestamp
  from table)x 
 where x.rnk=1