英文:
Not sure what I have wrong in this Oracle SQL query, I've asked my professor but the stuff he has told me to try isn't working
问题
以下是翻译好的部分:
"我正在尝试统计每月注册的学生人数,以下是我的当前查询:
如果我从选择中删除注册日期,那么它将起作用,显示正确的按月分隔的学生人数,但此时实际的月份列将不可见。我收到的错误信息是“SQL错误:
ORA-00979:不是一个GROUP BY表达式"
英文:
I am trying to get a count of how many students enrolled per month and this is my current query:
SELECT registrationdate AS "REGDATE", COUNT(*) AS "NUMSTUDENTS"
FROM registration r
INNER JOIN students s
ON r.studentid = s.studentid
GROUP BY To_Char(registrationdate, 'Month')
ORDER BY NUMSTUDENTS;
If I take registration date out of select then it will work, showing the correct number of students separated by month however the actual month column itself is missing at that point. Error I am getting is a "SQL Error:
> ORA-00979: not a GROUP BY expression
>
> 00979. 00000 - "not a GROUP BY expression""
答案1
得分: 1
你写的是:
SELECT registrationdate AS "REGDATE", ...
这不是一个聚合函数(比如COUNT),并且没有出现在GROUP BY 子句中。
你想要写的是:
SELECT To_Char(registrationdate, 'Month') AS "REGMONTH", ...
这个示例可能有助于你理解为什么需要这样做。
假设有三名学生在2月6日注册,另外两名学生在2月8日注册。
所以我们要报告一个单独的"February"行,其中包括五个事件的计数。
将这五个事件报告为发生在1日、28日、6日或8日是否有意义?
不是的。
我们是在月份级别进行汇总,报告的行应该反映这一点。
英文:
You wrote
SELECT registrationdate AS "REGDATE", ...
which is not an aggregate (like COUNT) and does not appear in the GROUP BY.
You wanted to write
SELECT To_Char(registrationdate, 'Month') AS "REGMONTH", ...
This may help you understand why.
Suppose three students registered on February 6th
and another pair on February 8th.
So we're reporting a single "February" row with a count of five.
Is it meaningful to report the five events as
happening on the 1st, or the 28th, or the 6th, or the 8th?
No.
We are aggregating at the month level,
and the report rows should reflect that.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论