英文:
Limit query result per unique columns combination
问题
我想要修改此查询,以限制结果每个组最多有2行(最新):
select
distinct clusterName,
aksNamespace,
acrName,
acrImageName,
acrImageVersion,
date
from
(
select
clusterName,
aksNamespace,
acrName,
acrImageName,
acrImageVersion,
date
from
aks_images
order by
acrImageName,
date desc
) as t
where
acrName = "storage"
order by
clusterName,
acrImageName,
date desc
当前结果:
clusterName | aksNamespace | acrName | acrImageName | acrImageVersion | date |
---|---|---|---|---|---|
dev | support | storage | app | f74581b | 17.02.2023 14:35 |
dev | support | storage | app | c6040a0 | 17.02.2023 7:45 |
dev | support | storage | app | 4410f39 | 16.02.2023 10:43 |
dev | abc | storage | qwer | 93241f1 | 15.02.2023 12:45 |
dev | abc | storage | qwer | 249b089 | 14.02.2023 13:15 |
dev | abc | storage | qwer | 1c40785 | 13.02.2023 13:30 |
prod | support | storage | app | 469a492 | 07.02.2023 14:15 |
test | support | storage | app | 07e22a6 | 17.02.2023 14:40 |
test | support | storage | app | daf975d | 17.02.2023 13:40 |
test | support | storage | app | 7e1a50b | 15.02.2023 13:10 |
test | support | storage | app | 8f27715 | 15.02.2023 9:35 |
期望结果:
clusterName | aksNamespace | acrName | acrImageName | acrImageVersion | date |
---|---|---|---|---|---|
dev | support | storage | app | f74581b | 17.02.2023 14:35 |
dev | support | storage | app | c6040a0 | 17.02.2023 7:45 |
dev | abc | storage | qwer | 93241f1 | 15.02.2023 12:45 |
dev | abc | storage | qwer | 249b089 | 14.02.2023 13:15 |
prod | support | storage | app | 469a492 | 07.02.2023 14:15 |
test | support | storage | app | 07e22a6 | 17.02.2023 14:40 |
test | support | storage | app | daf975d | 17.02.2023 13:40 |
Mysql 版本: 8.0.31
我将感激任何建议或解决方案。
英文:
I would like to modify this query to limit result to have max 2 rows (latest) per group:
select
distinct clusterName,
aksNamespace,
acrName,
acrImageName,
acrImageVersion,
date
from
(
select
clusterName,
aksNamespace,
acrName,
acrImageName,
acrImageVersion,
date
from
aks_images
order by
acrImageName,
date desc
) as t
where
acrName = "storage"
order by
clusterName,
acrImageName,
date desc
Current result:
clusterName | aksNamespace | acrName | acrImageName | acrImageVersion | `date` |
---|---|---|---|---|---|
dev | support | storage | app | f74581b | 17.02.2023 14:35 |
dev | support | storage | app | c6040a0 | 17.02.2023 7:45 |
dev | support | storage | app | 4410f39 | 16.02.2023 10:43 |
dev | abc | storage | qwer | 93241f1 | 15.02.2023 12:45 |
dev | abc | storage | qwer | 249b089 | 14.02.2023 13:15 |
dev | abc | storage | qwer | 1c40785 | 13.02.2023 13:30 |
prod | support | storage | app | 469a492 | 07.02.2023 14:15 |
test | support | storage | app | 07e22a6 | 17.02.2023 14:40 |
test | support | storage | app | daf975d | 17.02.2023 13:40 |
test | support | storage | app | 7e1a50b | 15.02.2023 13:10 |
test | support | storage | app | 8f27715 | 15.02.2023 9:35 |
Expected result:
clusterName | aksNamespace | acrName | acrImageName | acrImageVersion | `date` |
---|---|---|---|---|---|
dev | support | storage | app | f74581b | 17.02.2023 14:35 |
dev | support | storage | app | c6040a0 | 17.02.2023 7:45 |
dev | abc | storage | qwer | 93241f1 | 15.02.2023 12:45 |
dev | abc | storage | qwer | 249b089 | 14.02.2023 13:15 |
prod | support | storage | app | 469a492 | 07.02.2023 14:15 |
test | support | storage | app | 07e22a6 | 17.02.2023 14:40 |
test | support | storage | app | daf975d | 17.02.2023 13:40 |
Mysql version: 8.0.31
I'd be grateful for any advice or solutions.
答案1
得分: 1
在MySQL 8+中,我们可以使用 ROW_NUMBER()
:
WITH cte AS (
SELECT *, ROW_NUMBER() OVER (PARTITION BY clusterName, acrImageName
ORDER BY date DESC) rn
FROM aks_images
WHERE acrName = 'storage'
)
SELECT
clusterName,
aksNamespace,
acrName,
acrImageName,
acrImageVersion,
date
FROM cte
WHERE rn <= 2
ORDER BY
clusterName,
acrImageName,
date DESC;
英文:
On MySQL 8+, we can use ROW_NUMBER()
:
<!-- language: sql -->
WITH cte AS (
SELECT *, ROW_NUMBER() OVER (PARTITION BY clusterName, acrImageName
ORDER BY date DESC) rn
FROM aks_images
WHERE acrName = 'storage'
)
SELECT
clusterName,
aksNamespace,
acrName,
acrImageName,
acrImageVersion,
date
FROM cte
WHERE rn <= 2
ORDER BY
clusterName,
acrImageName,
date DESC;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论