Error: Invalid expression in the select list (not contained in either an aggregate function or the GROUP BY clause)

十年热恋 提交于 2019-11-29 16:46:31
swe

The usage of GROUP BY makes the engine group the records for you. To do grouping, you have to give advice to the RDBMS for each column, what it should do.

  • Group it? -> Add column to GROUP BY-Clause
  • Not group it? -> ok, what else?
    • ignore the column? remove it from your select-clause
    • Sum it? -> use SUM(mycol)
    • other aggregation functions can be found in the documentation

Additionally: In your case you try to group by EPS_ID, which is unique in each row. So a grouping by that column will return all rows, because there is nothing to group by. To group records, they have to have the same value.

Learn to use proper, explicit JOIN syntax.

Your problem is that all unaggregated columns need to be in the GROUP BY:

SELECT a.EPS_ID, MAX(b.C_NAME) AS XT, c.AY_YR_NAME, d.S_NAME, e.E_NAME
FROM TBLEXAMPLANNER_S_MSB a JOIN 
     TBLCLASS_MSB b
     ON a.EPS_CLASS_ID = b.C_ID JOIN
     TBLACADEMICYEAR_MSB c
     ON a.EPS_SESSION_ID = c.AY_ID JOIN
     TBLSUBJECTS_MSB d
     ON a.EPS_SUB_ID = d.S_ID JOIN 
     TBLEXAMTYPE_MSB e
     ON a.EPS_PE_ID = e.E_ID
GROUP BY a.EPS_ID, c.AY_YR_NAME, d.S_NAME, e.E_NAME;

Note: I would also recommend that you use table abbreviations for table aliases. So, ep for TBLEXAMPLANNER_S_MSB instead of a. Arbitrary table aliases make the query hard to follow.

Try

SELECT a.EPS_ID,c.AY_YR_NAME,d.S_NAME,e.E_NAME,MAX(b.C_NAME) AS XT ...
GROUP BY 1,2,3,4
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!