SQLSyntaxErrorException: ORA-00979: 不是 GROUP BY 表达式
Posted
技术标签:
【中文标题】SQLSyntaxErrorException: ORA-00979: 不是 GROUP BY 表达式【英文标题】:SQLSyntaxErrorException: ORA-00979: not a GROUP BY expression 【发布时间】:2020-03-31 14:54:28 【问题描述】:我在 java 中运行此代码时收到此错误“not a GROUP BY expression”,但在 SQL Developer 中运行良好。不知道我在哪里做错了。 请大家帮忙
错误:SQLSyntaxErrorException: ORA-00979: not a GROUP BY expression
SELECT data.portfolioid AS "CUSTOMER ID",
company.name AS "COMPANY",
trunc(data.startdate, :format) AS "USAGE DATE",
SUM(data.sessions) AS "SESSIONS",
SUM(data.zerosessions) AS "ZEROSESSIONS",
SUM(data.sms) AS "SMS",
SUM(data.VOICEDURATION) AS "VOICEDURATION",
SUM(data.totalbytes) AS "TOTAL BYTES",
SUM(data.billedbytes) AS "NORMALIZED BYTES",
sim.custom0 AS "CUSTOM1"
FROM
(SELECT portfolioid,
context,
startdate,
totalbytes,
billedbytes,
sessions,
zerosessions,
sms,
voiceduration,
sim.imsi
FROM nmma_abc_mo DATA
INNER JOIN nn_abc sim ON data.imsi = sim.imsi
WHERE portfolioid IN
(SELECT pf.id
FROM nc_vt_abc cb,
nn_abc pcb,
nn_abc pf
WHERE pcb.id = cb.productid
AND pf.id = pcb.portfolioid
AND pf.domainid = :domainid
AND (:portfolioid = -1
OR pf.id = :portfolioid) )
AND data.startdate BETWEEN to_date(:startDate, 'YYYY-MM-DD HH24:MI:SS') AND to_date(:endDate, 'YYYY-MM-DD HH24:MI:SS') ) DATA
LEFT JOIN nnp_abc portfolio ON portfolio.id = data.portfolioid
LEFT JOIN nnpc_abc company ON company.id = portfolio.companyid
LEFT JOIN nnsc_v_abc sim ON sim.imsi = data.imsi
WHERE sim.custom0 IS NOT NULL
GROUP BY data.portfolioid,
company.name,
trunc(data.startdate, :format),
sim.custom0
ORDER BY data.portfolioid
【问题讨论】:
一目了然,这可能是因为您的 :format 实际上是两个不同的变量,所以 Oracle 在编译时没有检查它们是否相等。尝试将日期格式移动到您的内部查询中,这样您只需要格式化一次。 这里有一些关于重复占位符的信息:Link 【参考方案1】:正如我在 cmets 中提到的,尝试将您的日期格式移动到内部查询中,以避免像这样重复占位符:
SELECT data.portfolioid AS "CUSTOMER ID",
company.name AS "COMPANY",
data.usage_date AS "USAGE DATE",
SUM(data.sessions) AS "SESSIONS",
SUM(data.zerosessions) AS "ZEROSESSIONS",
SUM(data.sms) AS "SMS",
SUM(data.VOICEDURATION) AS "VOICEDURATION",
SUM(data.totalbytes) AS "TOTAL BYTES",
SUM(data.billedbytes) AS "NORMALIZED BYTES",
sim.custom0 AS "CUSTOM1"
FROM (SELECT portfolioid,
CONTEXT,
trunc(startdate, :format) AS USAGE_DATE
totalbytes,
billedbytes,
sessions,
zerosessions,
sms,
voiceduration,
sim.imsi
FROM nmma_abc_mo data
INNER JOIN nn_abc sim ON data.imsi = sim.imsi
WHERE portfolioid IN (SELECT pf.id
FROM nc_vt_abc cb, nn_abc pcb, nn_abc pf
WHERE pcb.id = cb.productid
AND pf.id = pcb.portfolioid
AND pf.domainid = :domainid
AND (:portfolioid = -1 OR pf.id = :portfolioid))
AND data.startdate BETWEEN to_date(:startDate, 'YYYY-MM-DD HH24:MI:SS') AND
to_date(:endDate, 'YYYY-MM-DD HH24:MI:SS')
) data
LEFT JOIN nnp_abc portfolio ON portfolio.id = data.portfolioid
LEFT JOIN nnpc_abc company ON company.id = portfolio.companyid
LEFT JOIN nnsc_v_abc sim ON sim.imsi = data.imsi
WHERE sim.custom0 IS NOT NULL
GROUP BY data.portfolioid, company.name, data.usage_date, sim.custom0
ORDER BY data.portfolioid;
【讨论】:
以上是关于SQLSyntaxErrorException: ORA-00979: 不是 GROUP BY 表达式的主要内容,如果未能解决你的问题,请参考以下文章
Bug解决 java.sql.SQLSyntaxErrorException 异常
SQLSyntaxErrorException: ORA-00979: 不是 GROUP BY 表达式
java.sql.SQLSyntaxErrorException:表'mydb.alien'不存在
java.sql.SQLSyntaxErrorException:未知列
为啥 Bluemix dashDB 操作会引发 SQLCODE=-1667 的 SqlSyntaxErrorException?