如何将max()函数应用于数组列?
我有没有办法在每种流派上使用 max()
函数 imdb_score
?正如我想获得的最高 imdb_score
每类,这会给我显示标题。
title genres imdb_score
"Five Came Back: The Reference Films" "{documentation}" NULL
"Taxi Driver" "{crime,drama}" 8.3
"Monty Python and the Holy Grail" "{comedy,fantasy}" 8.2
"Life of Brian" "{comedy}" 8
"The Exorcist" "{horror}" 8.1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这显示了这部电影的分数最高:
db< “ Nofollow noreferrer“>在这里
i think 这就是您想要的。
用 。
关于
select> select
列表中的set-returning函数unnest()
:您没有定义如何打破领带。 (在类型中得分最高的多个电影。)因此,此查询选择了任意赢家。
关于
在
上截然不同:因为
imdb_score
可以是null
,它是至关重要的是将nulls last
添加到降序顺序。请参阅:如果您坚持
max()
...每年都会返回所有赢得电影:
慢得多且令人费解。要获得所有流派的获奖者,而是使用窗口函数等级() :
不会以无效分数消除获胜者 - 在没有其他分数的情况下。 (与
max()
的解决方案不同。This shows the film with the highest score per genre:
db<>fiddle here
I think that's what you want.
Unnest the array with
unnest()
.About the set-returning function
unnest()
in theSELECT
list:You did not define how to break ties. (Multiple films with equally the highest score in the genre.) So this query picks an arbitrary winner.
About
DISTINCT ON
:Since the
imdb_score
can beNULL
, it's crucial to addNULLS LAST
to the descending sort order. See:If you insist on
max()
...Also returning all winning films per genre:
Much slower and convoluted. To get all winners per genre, rather use the window function
rank()
:Does not eliminate winners with a NULL score - where no other score exists. (Unlike the solution with
max()
.)