sql group by 左连接

发布于 2024-08-28 23:55:17 字数 1275 浏览 12 评论 0原文

失败语句:错误:ORA-00979:不是 GROUP BY 表达式

 select org_division.name  , org_department.name , org_surveylog.division_code as divisionCode,org_surveylog.department_code as departmentCode , max(org_surveylog.actiondate) from org_surveylog 
left join org_division on (org_surveylog.division_code= org_division.division_code and org_surveylog.SURVEY_NUM= org_division.survey_num)
left join org_department on (org_surveylog.department_code = org_department.department_code and org_surveylog.SURVEY_NUM = org_department.survey_num) 
 group by org_surveylog.division_code,org_surveylog.department_code 

,但下面是可以的,

 select org_surveylog.division_code as divisionCode,org_surveylog.department_code as departmentCode , max(org_surveylog.actiondate) from org_surveylog 
left join org_division on (org_surveylog.division_code= org_division.division_code and org_surveylog.SURVEY_NUM= org_division.survey_num)
left join org_department on (org_surveylog.department_code = org_department.department_code and org_surveylog.SURVEY_NUM = org_department.survey_num) 
 group by org_surveylog.division_code,org_surveylog.department_code 

当我需要显示 org_division.name 、 org_department.name 的值时,如何使用带有左连接的 group by ?

fail statement:Error: ORA-00979: not a GROUP BY expression

 select org_division.name  , org_department.name , org_surveylog.division_code as divisionCode,org_surveylog.department_code as departmentCode , max(org_surveylog.actiondate) from org_surveylog 
left join org_division on (org_surveylog.division_code= org_division.division_code and org_surveylog.SURVEY_NUM= org_division.survey_num)
left join org_department on (org_surveylog.department_code = org_department.department_code and org_surveylog.SURVEY_NUM = org_department.survey_num) 
 group by org_surveylog.division_code,org_surveylog.department_code 

but below is ok

 select org_surveylog.division_code as divisionCode,org_surveylog.department_code as departmentCode , max(org_surveylog.actiondate) from org_surveylog 
left join org_division on (org_surveylog.division_code= org_division.division_code and org_surveylog.SURVEY_NUM= org_division.survey_num)
left join org_department on (org_surveylog.department_code = org_department.department_code and org_surveylog.SURVEY_NUM = org_department.survey_num) 
 group by org_surveylog.division_code,org_surveylog.department_code 

how to use group by with left join when i need to show value of org_division.name , org_department.name ?

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

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

发布评论

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

评论(1

孤星 2024-09-04 23:55:17

您需要将 GROUP BY 更改为

 group by   org_division.name , 
            org_department.name
            org_surveylog.division_code,
            org_surveylog.department_code 

From Oracle Select Statements : Select Statement With GROUP BY Clause

SELECT <column_name>, <aggregating_operation>
FROM <table_name>
GROUP BY <column_name>;

您您会注意到您需要在 GROUP BY 语句中包含非聚合列。

You need to change your GROUP BY to

 group by   org_division.name , 
            org_department.name
            org_surveylog.division_code,
            org_surveylog.department_code 

From Oracle Select Statements : Select Statement With GROUP BY Clause

SELECT <column_name>, <aggregating_operation>
FROM <table_name>
GROUP BY <column_name>;

You will notice that you need to include the non aggregated columns in the GROUP BY statement.

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