juntar e somar registros duplicados
01/12/2015
0
Ola amigos, estou fazendo uma consulta, e nao estou conseguindo o resultado que queria
select
'E' AS TIPO,
ent.cst, ent.CFOP,
ent.ICMS_ALIQUOTA,
sum(ent.ICMS_BASE) as VL_BC_ICMS,
sum(ent.ICMS_VALOR) as VL_ICMS,
sum(ent.SUB_BASE) as VL_BC_ICMS_ST,
sum(ent.SUB_VALOR) as VL_ICMS_ST,
sum(ent.ICMS_REDUCAO) as VL_RED_BC,
sum(ent.IPI_VALOR) as VL_IPI
FROM C000088 ent
WHERE (select DATA_EMISSAO from c000087 where CODIGO = ent.CODNOTA) >= :DT_INI and
(select DATA_EMISSAO from c000087 where CODIGO = ent.CODNOTA) <= :DT_FIM
and ent.CODNOTA = :CODIGO
group by TIPO, ent.cst, ent.CFOP, ent.ICMS_ALIQUOTA
[img]http://arquivo.devmedia.com.br/forum/imagem/190978-20151201-200354.png[/img]
no caso esses dois registros iguas "cst 000", "cfop 1202", e para somar os valores do mesmo e ficar so dois registros, somar os registros repetidos.
select
'E' AS TIPO,
ent.cst, ent.CFOP,
ent.ICMS_ALIQUOTA,
sum(ent.ICMS_BASE) as VL_BC_ICMS,
sum(ent.ICMS_VALOR) as VL_ICMS,
sum(ent.SUB_BASE) as VL_BC_ICMS_ST,
sum(ent.SUB_VALOR) as VL_ICMS_ST,
sum(ent.ICMS_REDUCAO) as VL_RED_BC,
sum(ent.IPI_VALOR) as VL_IPI
FROM C000088 ent
WHERE (select DATA_EMISSAO from c000087 where CODIGO = ent.CODNOTA) >= :DT_INI and
(select DATA_EMISSAO from c000087 where CODIGO = ent.CODNOTA) <= :DT_FIM
and ent.CODNOTA = :CODIGO
group by TIPO, ent.cst, ent.CFOP, ent.ICMS_ALIQUOTA
[img]http://arquivo.devmedia.com.br/forum/imagem/190978-20151201-200354.png[/img]
no caso esses dois registros iguas "cst 000", "cfop 1202", e para somar os valores do mesmo e ficar so dois registros, somar os registros repetidos.
Valtenci Santos
Curtir tópico
+ 0
Responder
Posts
01/12/2015
Cauê Nishijima
No group by vc tbm esta agrupando pela aliquota, e não vai agrupar pq a aliquota dos dois são diferentes.
Responder
08/12/2015
Valtenci Santos
mais ai se eu tirar a aliquota, ele vai agrupar e somar os registros ?
Responder
Clique aqui para fazer login e interagir na Comunidade :)