You may have installed it with max , but thatβs not why this happens, and is a bit hacked. Your problem is that your subquery, which translates into a single column, is not an aggregate query, min , max , sum , etc. And therefore it should be included in the group by clause. You fixed this by wrapping it in max , since the maximum of one value will always be constant.
However, since your subquery is itself an analytic query and will only ever return a single row, the obvious thing to do is use a Cartesian join to add it to your query. In explicit join syntax, this is called cross join .
select count(*) todas , sum(case when i.prioridade = 1 then 1 else 0 end) urgente , sum(case when i.prioridade = 2 then 1 else 0 end) alta , sum(case when i.prioridade = 3 then 1 else 0 end) normal , sum(case when i.prioridade = 4 then 1 else 0 end) baixa , naoAvaliados , sum(case when i.situacao = 'P' then 1 else 0 end) pendentes , sum(case when i.situacao = 'A' or i.situacao = 'I' then 1 else 0 end) iniciados from GMITEMOS i cross join (select count(*) as naoAvaliados from GMITEMOS j inner join GMCTLSLA k on k.os = j.cd_numero_os and k.item = j.item where j.situacao in ('A', 'I', 'P') and k.ordem = 99999 ) where i.situacao in ('A', 'I', 'P') and exists (select 1 from GMCTLSLA c where c.os = i.cd_numero_os and c.item = i.item )
A Cartesian compound has a bad reputation because it multiplies the number of rows on one side of the connection by the number of rows on the other. However, he uses it, especially in this case.
Ben
source share