gpt4 book ai didi

sql - 在 postgresql 中枚举

转载 作者:行者123 更新时间:2023-11-29 12:31:43 25 4
gpt4 key购买 nike

有什么方法可以在 postgresql 中枚举查询行?

我有这样的表:

Table players
id player_id game_id points

我想要这样的查询:

SELECT  game_id, <row_number in points group>, <array of ids> 
FROM players WHERE game_id = %s
GROUP BY game_id, points;

最佳答案

我不确定你在问什么。 “点组中的行号”是一个直截了当的 window function应用程序,但我不知道“id 数组”是什么意思。

给定的日期是这样的:

 id | player_id | game_id | points 
----+-----------+---------+--------
1 | 1 | 1 | 0
2 | 1 | 2 | 1
3 | 1 | 3 | 5
4 | 2 | 1 | 1
5 | 2 | 2 | 0
6 | 2 | 3 | 0
7 | 3 | 1 | 2
8 | 3 | 2 | 3
9 | 3 | 3 | 1

你可以得到每场比赛的排名:

select game_id, player_id, points,
rank() over (partition by game_id order by points desc)
from players

这会给你这样的输出:

 game_id | player_id | points | rank 
---------+-----------+--------+------
1 | 3 | 2 | 1
1 | 2 | 1 | 2
1 | 1 | 0 | 3
2 | 3 | 3 | 1
2 | 1 | 1 | 2
2 | 2 | 0 | 3
3 | 1 | 5 | 1
3 | 3 | 1 | 2
3 | 2 | 0 | 3

关于sql - 在 postgresql 中枚举,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9172621/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com