我有这个MySQL查询,它完全适用于我需要它做的事情。但是,我需要将其转换为postgres安装,并且似乎遇到了一些麻烦。查询如下:
SELECT r.studno, r.sdate,r.subject,
sum(exam1) as exam1,sum(exam2) as exam2,sum(asgn1) as asgn1,sum(asgn2) as asgn2,
sum(proj1) as proj1,sum(proj2) as proj2,sum(pract1) as pract1, sum(pract2) as pract2,
r.overallmark, r.result, r.credits, r.corlevel, r.nceaaward, r.gpa, r.overallresult
FROM exam_results r WHERE r.studno = :studno AND r.sdate = :sdate GROUP BY r.studno, r.subject
我已尝试在postgres中运行此操作,但出现以下错误:
2014-06-23 11:56:54 IST ERROR: column "r.sdate" must appear in the GROUP BY clause or be used in an aggregate function at character 8
我如何解决这个问题?
答案 0 :(得分:1)
您的查询使用的MySQL扩展程序允许select
中的列不在group by
中。您可以通过将所有此类列包装在聚合函数(例如max()
:
SELECT r.studno, r.sdate, r.subject,
sum(exam1) as exam1,sum(exam2) as exam2,sum(asgn1) as asgn1,sum(asgn2) as asgn2,
sum(proj1) as proj1,sum(proj2) as proj2,sum(pract1) as pract1, sum(pract2) as pract2,
max(r.overallmark), max(r.result), max(r.credits),
max(r.corlevel), max(r.nceaaward), max(r.gpa), max(r.overallresult)
FROM exam_results r
WHERE r.studno = :studno AND r.sdate = :sdate
GROUP BY r.studno, r.sdate, r.subject;