Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
聚合列上的PostgreSQL RANK()函数_Postgresql_Aggregate Functions_Rank - Fatal编程技术网

聚合列上的PostgreSQL RANK()函数

聚合列上的PostgreSQL RANK()函数,postgresql,aggregate-functions,rank,Postgresql,Aggregate Functions,Rank,我正在构造一个非常复杂的查询,在这里我试图加载用户的聚合点以及他们的排名。我找到了RANK()函数,它可以帮助我实现这一点,但无法让它工作 以下是在没有排名的情况下运行的查询: SELECT users.*, SUM(received_points.count) AS pts FROM users LEFT JOIN received_points ON received_points.user_id = users.id AND ...other joining conditions...

我正在构造一个非常复杂的查询,在这里我试图加载用户的聚合点以及他们的排名。我找到了RANK()函数,它可以帮助我实现这一点,但无法让它工作

以下是在没有排名的情况下运行的查询:

SELECT users.*, SUM(received_points.count) AS pts 
FROM users 
LEFT JOIN received_points ON received_points.user_id = users.id AND ...other joining conditions... 
GROUP BY users.id 
ORDER BY pts DESC NULLS LAST
现在,我还想选择秩-但使用秩函数的这种方式不起作用:

SELECT users.*, SUM(received_points.count) AS pts, 
    RANK() OVER (ORDER BY pts DESC NULLS LAST) AS position
FROM users 
LEFT JOIN received_points ON received_points.user_id = users.id AND ...other joining conditions... 
GROUP BY users.id 
ORDER BY pts DESC NULLS LAST
它告诉:
PG::UndefinedColumn:ERROR:列“pts”不存在

我想我把窗口函数的整个概念搞错了。如何选择按聚合值排序的用户排名,如上例中的
pts


我知道我可以在以后手动分配排名,但如果我还想根据查询中的users.name筛选行,并且仍然在常规(未筛选)排行榜中获得用户的排名,该怎么办。。。?不知道我是否清楚…

您不能在此处使用
pts
,因为别名尚不存在(您不能在同一
中引用别名,请选择它已定义)
RANK()结束(按总和排序(接收到的_points.count)DESC NULLS LAST)
应该可以正常工作。谢谢,伙计,现在看起来很棒了!)