如何按不同的列选择最后一个时间戳?



假设有这样的表:

| user_id | location_id | datetime            | other_field |
| ------- | ----------- | ------------------- | ----------- |
| 12      | 1           | 2020-02-01 10:00:00 | asdqwe      |
| 12      | 1           | 2020-02-01 10:30:00 | asdqwe      |
| 12      | 2           | 2020-02-01 10:40:00 | asdqwe      |
| 12      | 2           | 2020-02-01 10:50:00 | asdqwe      |
| 13      | 1           | 2020-02-01 10:10:00 | asdqwe      |
| 13      | 1           | 2020-02-01 10:20:00 | asdqwe      |
| 14      | 3           | 2020-02-01 09:00:00 | asdqwe      |

我想选择每个不同user_idlocation_id的最后datetime。这就是我正在寻找的结果:

| user_id | location_id | datetime            | other_field |
| ------- | ----------- | ------------------- | ----------- |
| 12      | 1           | 2020-02-01 10:30:00 | asdqwe      |
| 12      | 2           | 2020-02-01 10:50:00 | asdqwe      |
| 13      | 1           | 2020-02-01 10:20:00 | asdqwe      |
| 14      | 3           | 2020-02-01 09:00:00 | asdqwe      |

以下是表说明:

CREATE TABLE mykeyspace.mytable (
user_id int,
location_id int,
datetime timestamp,
other_field text,
PRIMARY KEY ((user_id, location_id, other_field), datetime)
) WITH CLUSTERING ORDER BY (datetime ASC)
AND read_repair_chance = 0.0
AND dclocal_read_repair_chance = 0.1
AND gc_grace_seconds = 864000
AND bloom_filter_fp_chance = 0.01
AND caching = { 'keys' : 'ALL', 'rows_per_partition' : 'NONE' }
AND comment = ''
AND compaction = { 'class' : 'org.apache.cassandra.db.compaction.SizeTieredCompactionStrategy', 'max_threshold' : 32, 'min_threshold' : 4 }
AND compression = { 'chunk_length_in_kb' : 64, 'class' : 'org.apache.cassandra.io.compress.LZ4Compressor' }
AND default_time_to_live = 0
AND speculative_retry = '99PERCENTILE'
AND min_index_interval = 128
AND max_index_interval = 2048
AND crc_check_chance = 1.0
AND cdc = false;

对于这些事情,CQL 有"每个分区限制"子句(在 Cassandra 3.6+ IIRC 中可用(。但是要在表上使用,您需要将表定义更改为CLUSTERING ORDER BY (datetime DESC),然后您可以编写:

select * from prospacedb.quarter_utilisation per partition limit 1;

并获取具有您拥有的每个分区键的最新时间戳的行。

最新更新