具有 MAX 聚合函数的 CASE 表达式 Oracle

发布于 2024-12-20 00:31:25 字数 701 浏览 0 评论 0原文

使用 Oracle,我选择了 title_id 及其关联的出版月份:

SELECT title_id, 
CASE EXTRACT(month FROM pubdate) 
    WHEN 1 THEN 'Jan'
    WHEN 2 THEN 'Feb'
    WHEN  3 THEN 'Mar'
    WHEN  4 THEN 'Apr'
    WHEN  5 THEN 'May'
    WHEN  6 THEN 'Jun'
    WHEN  7 THEN 'Jul'
    WHEN  8 THEN 'Aug'
    WHEN  9 THEN 'Sep'
    WHEN  10 THEN 'Oct'
    WHEN  11 THEN 'Nov'
    ELSE 'Dec'
    END MONTH 
FROM TITLES;

使用以下语句:

SELECT MAX(Most_Titles) 
FROM (SELECT count(title_id) Most_Titles, month 
       FROM (SELECT title_id, extract(month FROM pubdate) AS MONTH FROM titles) GROUP BY month);

我能够确定出版图书数量最多的月份。

有没有办法连接这两个语句,以便我可以将月份的文本等效项与最大标题数关联起来?

Using Oracle, I have selected the title_id with its the associated month of publication with:

SELECT title_id, 
CASE EXTRACT(month FROM pubdate) 
    WHEN 1 THEN 'Jan'
    WHEN 2 THEN 'Feb'
    WHEN  3 THEN 'Mar'
    WHEN  4 THEN 'Apr'
    WHEN  5 THEN 'May'
    WHEN  6 THEN 'Jun'
    WHEN  7 THEN 'Jul'
    WHEN  8 THEN 'Aug'
    WHEN  9 THEN 'Sep'
    WHEN  10 THEN 'Oct'
    WHEN  11 THEN 'Nov'
    ELSE 'Dec'
    END MONTH 
FROM TITLES;

Using the statement:

SELECT MAX(Most_Titles) 
FROM (SELECT count(title_id) Most_Titles, month 
       FROM (SELECT title_id, extract(month FROM pubdate) AS MONTH FROM titles) GROUP BY month);

I was able to determine the month with the maximum number of books published.

Is there a way to join the two statements so that I can associate the month's text equivalent with the maximum number of titles?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

绝不服输 2024-12-27 00:31:25

为了将月份转换为字符串,我不会使用 CASE 语句,而是使用 TO_CHAR。您可以使用分析函数对结果进行排名,以获得出版图书最多的月份。

SELECT num_titles,
       to_char( publication_month, 'Mon' ) month_str
  FROM (SELECT count(title_id) num_titles,
               trunc(pubdate, 'MM') publication_month,
               rank() over (order by count(title_id) desc) rnk
          FROM titles
         GROUP BY trunc(pubdate, 'MM'))
 WHERE rnk = 1

一些额外的注意事项

  • 如果有两个月与最多的出版物相关,则此查询将返回两行。如果您希望 Oracle 任意选择一个,您可以使用 row_number 分析函数而不是 rank
  • 如果表中的 PUBDATE 列仅包含图书出版当月第一天的午夜日期,您可以消除 PUBDATE< 上的 trunc /代码> 列。

In order to convert a month to a string, I wouldn't use a CASE statement, I'd just use a TO_CHAR. And you can use analytic functions to rank the results to get the month with the most books published.

SELECT num_titles,
       to_char( publication_month, 'Mon' ) month_str
  FROM (SELECT count(title_id) num_titles,
               trunc(pubdate, 'MM') publication_month,
               rank() over (order by count(title_id) desc) rnk
          FROM titles
         GROUP BY trunc(pubdate, 'MM'))
 WHERE rnk = 1

A couple of additional caveats

  • If there are two months that are tied with the most publications, this query will return both rows. If you want Oracle to arbitrarily pick one, you can use the row_number analytic function rather than rank.
  • If the PUBDATE column in your table only has dates of midnight on the first of the month where the book is published, you can eliminate the trunc on the PUBDATE column.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文