Select 分组查询的前 `n` 行
Select first `n` rows of a grouped query
我正在将 PostgreSQL 与 SQLAlchemy 结合使用
我有一个 table 的 GPS 指标,格式为:
SELECT * FROM user_gps_location;
我的输出:
| id | user_id | entry_time | lat | lng | accuracy | altitude | speed |
| 1 | 54 | 2020-07-24 14:08:30.000000 | 54.42184220 | -110.21029370 | 41.42 | 512.40 | 0.07 |
| 2 | 54 | 2020-07-24 22:20:12.000000 | 54.42189750 | -110.21038070 | 13.00 | 512.60 | 0.00 |
| 3 | 26 | 2020-07-27 13:51:11.000000 | 54.41453910 | -110.20775990 | 1300.00 | 0.00 | 0.00 |
| 4 | 26 | 2020-07-27 22:59:00.000000 | 54.42122590 | -110.20959960 | 257.52 | 509.10 | 0.00 |
| 5 | 26 | 2020-07-28 13:54:12.000000 | 54.42185280 | -110.21025010 | 81.45 | 510.20 | 0.00 |
...
我需要能够回答“自”以来每个用户的最新 5 个条目是什么”这个问题,按 entry_time
排序
现在我只有一个基本的查询:
select *
from user_gps_location
where user_id in (select distinct user_id
from user_gps_location
where entry_time > '2020-09-01')
and entry_time > '2020-09-01';
应用限制不会达到我想要的效果。我假设我需要使用分组和 window 函数 (?),但我不理解它们。
row_number
函数正是您要找的:
SELECT *
FROM (SELECT *, ROW_NUMBER() OVER (PARTITION BY user_id ORDER BY entry_time DESC) AS rn
FROM user_gps_location
WHERE entry_time > '2020-09-01') t
WHERE rn <= 5
你可以使用FETCH FIRST N ROWS ONLY
select * from user_gps_location
where entry_time > '2020-09-01'
order by entry_time desc
fetch first 5 rows only
我正在将 PostgreSQL 与 SQLAlchemy 结合使用
我有一个 table 的 GPS 指标,格式为:
SELECT * FROM user_gps_location;
我的输出:
| id | user_id | entry_time | lat | lng | accuracy | altitude | speed | | 1 | 54 | 2020-07-24 14:08:30.000000 | 54.42184220 | -110.21029370 | 41.42 | 512.40 | 0.07 | | 2 | 54 | 2020-07-24 22:20:12.000000 | 54.42189750 | -110.21038070 | 13.00 | 512.60 | 0.00 | | 3 | 26 | 2020-07-27 13:51:11.000000 | 54.41453910 | -110.20775990 | 1300.00 | 0.00 | 0.00 | | 4 | 26 | 2020-07-27 22:59:00.000000 | 54.42122590 | -110.20959960 | 257.52 | 509.10 | 0.00 | | 5 | 26 | 2020-07-28 13:54:12.000000 | 54.42185280 | -110.21025010 | 81.45 | 510.20 | 0.00 | ...
我需要能够回答“自”以来每个用户的最新 5 个条目是什么”这个问题,按 entry_time
现在我只有一个基本的查询:
select *
from user_gps_location
where user_id in (select distinct user_id
from user_gps_location
where entry_time > '2020-09-01')
and entry_time > '2020-09-01';
应用限制不会达到我想要的效果。我假设我需要使用分组和 window 函数 (?),但我不理解它们。
row_number
函数正是您要找的:
SELECT *
FROM (SELECT *, ROW_NUMBER() OVER (PARTITION BY user_id ORDER BY entry_time DESC) AS rn
FROM user_gps_location
WHERE entry_time > '2020-09-01') t
WHERE rn <= 5
你可以使用FETCH FIRST N ROWS ONLY
select * from user_gps_location
where entry_time > '2020-09-01'
order by entry_time desc
fetch first 5 rows only