0
I’m with a query that is giving problem, I believe the problem occurs for not allow to use the function max
, within the function sum
, am I correct?
How can I get around this? Because I need to add up all the values except the last one row
.
Follow the query:
"SELECT new project.domain.vo.MediaPagVo(" +
"a.nomeUsuario, " +
"a.cpfUsuario, " +
"a.cnpjUnidadeUsuario, " +
"a.nomeUnidadeUsuario, " +
"a.codigoGrupoUsuario, " +
"a.grupo.id AS idGrupo, " +
"CASE " +
"WHEN MAX(a.pre) - MIN(a.pre) > 0 " +
"THEN ((MAX(a.pre) - MIN(a.pre)) / SUM(CASE WHEN max(a.dtRequisicao) > a.dtRequisicao THEN a.valorTotal ELSE 0 END)) " +
"ELSE ((MAX(a.pos) - MIN(a.pos)) / SUM(CASE WHEN max(a.dtRequisicao) >= a.dtRequisicao THEN a.valorTotal ELSE 0 END)) " +
"END AS mediaConsumo, " +
"CASE " +
"WHEN MAX(a.pre) - MIN(a.pre) > 0 " +
"THEN " + Tipo.PRE.getValue() + " " +
"ELSE " + Tipo.POS.getValue() + " " +
"END AS tipoPag) " +
"FROM " +
" Pagamento a " +
"WHERE " +
"a.status = " + StatusPagamento.Concluido.getValue() + " AND " +
"a.dataRequisicao BETWEEN :inicio and :fim AND " +
"( :idGrupo IS NULL OR a.grupo.id = :idGrupo) " +
"GROUP BY " +
" a.nomeUsuario, a.cpfUsuario, a.cnpjUnidadeUsuario, a.nomeUnidadeUsuario, a.codigoGrupoUsuario, a.grupo.id, " +
"HAVING (MAX(a.pos) - MIN(a.pos) > 0 OR MAX(a.pre) - MIN(a.pre) > 0) AND " +
"( :tipoConsumo IS NULL OR " +
":tipoConsumo = " + Tipo.PRE.getValue() + " AND " +
"(MAX(a.pre) - MIN(a.pre) > 0) " +
"OR :tipoConsumo = " + Tipo.POS.getValue() + " AND " +
"(MAX(a.pos) - MIN(a.pos) > 0) ) ";
The error started to occur after I modified this excerpt by adding the case
within the sum
:
"CASE WHEN MAX(a.pre) - MIN(a.pre) > 0 " +
"THEN ((MAX(a.pre) - MIN(a.pre)) / SUM(CASE WHEN max(a.dtRequisicao) > a.dtRequisicao THEN a.valorTotal ELSE 0 END)) " +
"ELSE ((MAX(a.pos) - MIN(a.pos)) / SUM(CASE WHEN max(a.dtRequisicao) >= a.dtRequisicao THEN a.valorTotal ELSE 0 END)) " +
"END AS mediaPag, "
The error returned by java:
Could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet
Caused by: java.sql.SQLSyntaxErrorException: ORA-00937: não é uma função de grupo de grupo simples
My goal was to add up all the payments of that period, except the last one. Unfortunately, I could not only touch the filter, because the max(a.pre), max(a.pos)
may be from the last payment.
Does anyone have any idea?
I appreciate the help, but if I put
a.dtRequisicao, a.valorTotal
in thegroup by
, it will not group by these fields either, affecting the sum that should sum all values independent of the request date (except for the last)?– Fernando Bittencourt
@Fernandobittencourt, I believe you would then need to use a subselect where the case is, making a JOIN with some value present in GROUP BY, something thus. I cannot give more details of the solution itself because I would need to understand the whole context of your query (which would be another question, in my view).
– Dherik
I chose to completely refactor the query, it was already at a level of complexity difficult to treat.
– Fernando Bittencourt
@Fernandobittencourt, yes, it seemed very complex indeed :)
– Dherik