如何将max()函数应用于数组列?

发布于 2025-02-13 20:56:19 字数 539 浏览 0 评论 0 原文

我有没有办法在每种流派上使用 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

Is there a way for me use the MAX() function for the imdb_score on each genres (ARRAYS)? As I would like to get the highest imdb_score per genre that would show me the title.

    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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦罢 2025-02-20 20:56:19

这显示了这部电影的分数最高:

SELECT DISTINCT ON (genre) *
FROM  (
   SELECT unnest(genres) AS genre, title, imdb_score
   FROM   tbl
   ) sub
ORDER  BY genre, imdb_score DESC NULLS LAST;

db< “ Nofollow noreferrer“>在这里

i think 这就是您想要的。


关于 select> select 列表中的set-returning函数 unnest()

您没有定义如何打破领带。 (在类型中得分最高的多个电影。)因此,此查询选择了任意赢家。

关于上截然不同:

因为 imdb_score 可以是 null ,它是至关重要的是将 nulls last 添加到降序顺序。请参阅:


如果您坚持 max() ...
每年都会返回所有赢得电影:

WITH flat AS (
   SELECT unnest(genres) AS genre, imdb_score, title
   FROM   tbl
   )
SELECT genre, title, imdb_score
FROM  (
   SELECT genre, max(imdb_score) AS imdb_score
   FROM   flat
   GROUP  BY 1
   ) sub2
JOIN   flat f USING (genre, imdb_score)
ORDER  BY genre, title;

慢得多且令人费解。要获得所有流派的获奖者,而是使用窗口函数等级()

SELECT genre, title, imdb_score
FROM  (
   SELECT genre, title, imdb_score
        , rank() OVER (PARTITION BY genre ORDER BY imdb_score DESC NULLS LAST) AS rnk
   FROM  (
      SELECT unnest(genres) AS genre, imdb_score, title
      FROM   tbl
      ) sub
   ) sub2
WHERE  rnk = 1
ORDER  BY genre, title;

不会以无效分数消除获胜者 - 在没有其他分数的情况下。 (与 max()的解决方案不同。

This shows the film with the highest score per genre:

SELECT DISTINCT ON (genre) *
FROM  (
   SELECT unnest(genres) AS genre, title, imdb_score
   FROM   tbl
   ) sub
ORDER  BY genre, imdb_score DESC NULLS LAST;

db<>fiddle here

I think that's what you want.

Unnest the array with unnest().
About the set-returning function unnest() in the SELECT 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 be NULL, it's crucial to add NULLS LAST to the descending sort order. See:


If you insist on max() ...
Also returning all winning films per genre:

WITH flat AS (
   SELECT unnest(genres) AS genre, imdb_score, title
   FROM   tbl
   )
SELECT genre, title, imdb_score
FROM  (
   SELECT genre, max(imdb_score) AS imdb_score
   FROM   flat
   GROUP  BY 1
   ) sub2
JOIN   flat f USING (genre, imdb_score)
ORDER  BY genre, title;

Much slower and convoluted. To get all winners per genre, rather use the window function rank():

SELECT genre, title, imdb_score
FROM  (
   SELECT genre, title, imdb_score
        , rank() OVER (PARTITION BY genre ORDER BY imdb_score DESC NULLS LAST) AS rnk
   FROM  (
      SELECT unnest(genres) AS genre, imdb_score, title
      FROM   tbl
      ) sub
   ) sub2
WHERE  rnk = 1
ORDER  BY genre, title;

Does not eliminate winners with a NULL score - where no other score exists. (Unlike the solution with max().)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文