Erro de select com procedure

29/11/2021

0

Estou executando o comando abaixo em Firebird 3.0 (ibexpert):

select F.NOPE as CFOP, F.NFISCAL as NDOC, F.DATA as DEMI, F.PERC_ICMS, F.PERC_IPI, F.PERC_PIS, F.PERC_COFINS, sum(
case
when F.TNOTA = 'E' then abs(F.TOTAL_VENDIDO + coalesce(F.VR_IPI, 0))
else (F.TOTAL_VENDIDO + coalesce(F.VR_IPI, 0))
end) as VR_CONTABIL,
sum(F.VR_BC_ICMS) as VR_BCICMS, sum(
case
when F.TNOTA = 'E' then F.VR_ICMS
else (F.VR_ICMS)
end) as VR_ICMS,
sum(F.VR_BC_IPI) as VR_BC_IPI, sum(F.VR_IPI) as VR_IPI, sum(
case
when F.TP_SIT_TRIB_IPI = '9' then 0.00
else case
when F.TNOTA = 'E' then abs(F.VR_IPI)
else (F.VR_IPI)
end
end) as VR_IPI,
sum(
case
when F.TP_SIT_TRIB_IPI = '9' then 0.00
else case
when F.TNOTA = 'E' then abs(F.VR_PIS)
else (F.VR_PIS)
end
end) as VR_PIS,
sum(
case
when F.TP_SIT_TRIB_IPI = '9' then 0.00
else case
when F.TNOTA = 'E' then abs(F.VR_COFINS)
else (F.VR_COFINS)
end
end) as VR_COFINS,
'1' as CODIMPICMS, '2' as CODIMPIPI, '3' as CODIMPPIS, '4' as CODIMPCOFINS, '2' as CODTIPOIMPFISCAL,
case
when F.PERC_ICMS = 4 then '4%'
when F.PERC_ICMS = 7 then '7%'
when F.PERC_ICMS = 12 then '12%'
when F.PERC_ICMS = 13.3 then '13,3%'
when F.PERC_ICMS = 18 then '18%'
else 'Outras'
end AL_ICMS,
decode(F.PERC_IPI, null, 'Outras', F.PERC_IPI || '%') as AL_IPI,
case
when F.PERC_PIS = 1.65 then '1,65%'
when F.PERC_PIS = 0.65 then '0,65%'
else 'Outras'
end AL_PIS,
case
when F.PERC_COFINS = 7.60 then '7,60%'
when F.PERC_COFINS = 3 then '3%'
else 'Outras'
end AL_COFINS,
F.COD_EMPRESA, F.PRODUTO
from SP_VENDA_ITENS('01.01.1900', '31.12.2999', 'E', null) F
where F.ORIGEM = 'Itens-NF' and
F.NOPE > 0 and
F.INTESCR = 'S'
group by 1, 2, 3, 4, 5, 6, 7, 21, 22, 23, 24
order by 1, 2

Porém, estou recebendo como retorno a seguinte mensagem:

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

A ordem entre o where, group by e order by está correta, alguém sabe como posso corrigir esse erro?
Delmar Machado

Delmar Machado

Responder

Post mais votado

29/11/2021

todos (todos) os campos que não estão numa função de agregação ( sum, avg, count... etc ) precisam constar na lista do group by.
você tem ali, ao menos,
F.PERC_PIS,
F.PERC_COFINS,
F.COD_EMPRESA,
F.PRODUTO

Emerson Nascimento

Emerson Nascimento
Responder

Mais Posts

29/11/2021

Delmar Machado

Deu certo, pessoal. A view ficou conforme abaixo:

create or alter view BI_FISCAL (
PRODUTO,
QTDE,
VUNIT,
VTOTAL,
VCUSTO,
VICM,
AL_ICMS,
VIPI,
AL_IPI,
VR_PIS,
AL_PIS,
VR_COFINS,
AL_COFINS,
CODIMPICMS,
CODIMPIPI,
CODIMPPIS,
CODIMPCOFINS,
CODTIPOIMPFISCAL,
DCHEG,
CFOP,
COD_EMPRESA,
NOTA,
TP_IMPOSTO,
TP_SE)
as

-- Compras
select F.PRODUTO, F.QTDE, F.VUNIT, F.VTOTAL, F.VR_CUSTO as VCUSTO, sum(
case F.TP_SIT_TRIB
when '3' then 0.00
else F.VR_ICMS
end) as VICM,
case
when F.PERC_ICMS = 4 then '4%'
when F.PERC_ICMS = 7 then '7%'
when F.PERC_ICMS = 12 then '12%'
when F.PERC_ICMS = 13.3 then '13,3%'
when F.PERC_ICMS = 18 then '18%'
else 'Outras'
end AL_ICMS,
sum(
case
when F.TP_SIT_TRIB_IPI = '9' then 0.00
else F.VR_IPI
end) as VIPI,
decode(F.PERC_IPI, null, 'Outras', F.PERC_IPI || '%') as AL_IPI, sum(F.VR_PIS) as VR_PIS,
case
when F.PERC_PIS = 1.65 then '1,65%'
when F.PERC_PIS = 0.65 then '0,65%'
else 'Outras'
end AL_PIS,
sum(F.VR_COFINS) as VR_COFINS,
case
when F.PERC_COFINS = 7.60 then '7,60%'
when F.PERC_COFINS = 3 then '3%'
else 'Outras'
end AL_COFINS,
'1' as CODIMPICMS, '2' as CODIMPIPI, '3' as CODIMPPIS, '4' as CODIMPCOFINS, '1' as CODTIPOIMPFISCAL,
F.DATA as DCHEG, F.NOPE as CFOP, F.COD_EMPRESA, F.NDOC as NOTA, 'CREDITO (ENTRADAS)' as TP_IMPOSTO,
'E' as TP_SE
from SP_COMPRA_ITENS('01.01.1900', '31.12.2999') F
where F.ORIGEM = 'Itens-NF' and
F.NOPE > 0 and
F.INTESCR = 'S'
group by 1, 2, 3, 4, 5, 7, 9, 11, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24

union all

-- Vendas
select F.PRODUTO, F.QTDE, F.VUNIT, F.VTOTAL, F.VR_CUSTO as VCUSTO, sum(
case
when F.TNOTA = 'E' then F.VR_ICMS
else (F.VR_ICMS)
end) as VICM,
case
when F.PERC_ICMS = 4 then '4%'
when F.PERC_ICMS = 7 then '7%'
when F.PERC_ICMS = 12 then '12%'
when F.PERC_ICMS = 13.3 then '13,3%'
when F.PERC_ICMS = 18 then '18%'
else 'Outras'
end AL_ICMS,
sum(
case
when F.TP_SIT_TRIB_IPI = '9' then 0.00
else case
when F.TNOTA = 'E' then abs(F.VR_IPI)
else (F.VR_IPI)
end
end) as VIPI,
decode(F.PERC_IPI, null, 'Outras', F.PERC_IPI || '%') as AL_IPI, sum(F.VR_PIS) as VR_PIS,
case
when F.PERC_PIS = 1.65 then '1,65%'
when F.PERC_PIS = 0.65 then '0,65%'
else 'Outras'
end AL_PIS,
sum(F.VR_COFINS) as VR_COFINS,
case
when F.PERC_COFINS = 7.60 then '7,60%'
when F.PERC_COFINS = 3 then '3%'
else 'Outras'
end AL_COFINS,
'1' as CODIMPICMS, '2' as CODIMPIPI, '3' as CODIMPPIS, '4' as CODIMPCOFINS, '2' as CODTIPOIMPFISCAL,
F.DATA as DCHEG, F.NOPE as CFOP, F.COD_EMPRESA, F.NFISCAL as NOTA, 'DEBITO (SAIDAS)' as TP_IMPOSTO,
'S' as TP_SE
from SP_VENDA_ITENS('01.01.1900', '31.12.2999', 'E', null) F
inner join SERIE_NF S on F.SEQSERIE = S.SEQSERIE
inner join MODELO_NF MN on MN.SEQMODELO = S.SEQMODELO
where F.ORIGEM = 'Itens-NF' and
F.NOPE > 0 and
F.INTESCR = 'S' and
MN.MODELO = '55' and
S.SERIE not like '%SERV%'
group by 1, 2, 3, 4, 5, 7, 9, 11, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24;
Responder

30/11/2021

Emerson Nascimento

eu só evitaria usar o índice do campo. sempre que possível utilize o nome do campo, assim você evita problemas no agrupamento se precisar alterar a ordem de apresentação dos campos.
Responder

30/11/2021

Delmar Machado

Índice do campo? O que seria isso Emerson, poderia me dar um exemplo com base em meu comando por favor?
Responder

30/11/2021

Emerson Nascimento

sim, a posição do campo na instrução.
group by 1, 2, 3, 4, 5, 7, 9, 11, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24;
recomento trocar por:
group by
    F.PRODUTO, F.QTDE, F.VUNIT, F.VTOTAL, F.VR_CUSTO,
    7, 9, 11, 13,
    F.DATA, F.NOPE, F.COD_EMPRESA, F.NDOC

se o teu banco aceitar, pode ser assim:
group by
    F.PRODUTO, F.QTDE, F.VUNIT, F.VTOTAL, F.VR_CUSTO,
    AL_ICMS, AL_IPI, AL_PIS, AL_COFINS,
    F.DATA, F.NOPE, F.COD_EMPRESA, F.NDOC

desse modo, se te pedirem pra incluir um novo campo no resultado, você não precisará se preocupar com a posição na qual esse novo campo será inserido.
Responder

Assista grátis a nossa aula inaugural

Assitir aula

Saiba por que programar é uma questão de
sobrevivência e como aprender sem riscos

Assistir agora

Utilizamos cookies para fornecer uma melhor experiência para nossos usuários, consulte nossa política de privacidade.

Aceitar