具有 MAX 聚合函数的 CASE 表达式 Oracle
使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为了将月份转换为字符串,我不会使用
CASE
语句,而是使用TO_CHAR
。您可以使用分析函数对结果进行排名,以获得出版图书最多的月份。一些额外的注意事项
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 aTO_CHAR
. And you can use analytic functions to rank the results to get the month with the most books published.A couple of additional caveats
row_number
analytic function rather thanrank
.PUBDATE
column in your table only has dates of midnight on the first of the month where the book is published, you can eliminate thetrunc
on thePUBDATE
column.