Você está na página 1de 9

SCRIPT

NFCe GESMED NOVEMBRO 2023


create table teste.nfce_gesmed_202311 as

WITH dados AS

(SELECT trim(regexp_replace(regexp_replace(regexp_replace(lower(i.prod.xprod), '\s*[-./\|!


@&+(){}$%=,#?ºª°;<>:"0123456789]+\s*', ' '),"[\[\]']", ""), '\s{2,}', ' ')) descricao,

(CASE

WHEN i.imposto.icms.resumo.vicms > 0 THEN round(i.imposto.icms.resumo.picms * (1


- nvl(i.imposto.icms.resumo.predbc, 0)/100), 0)

ELSE 0

END) aliq_efetiva, /*(CASE

WHEN (nvl(i.prod.vprod, 0) + nvl(i.prod.vfrete, 0) + nvl(i.prod.vseg, 0) +


nvl(i.prod.voutro, 0) - nvl(i.prod.vdesc, 0)) = 0 THEN NULL

ELSE round(100 * nvl(i.imposto.icms.resumo.vicms, 0) / (nvl(i.prod.vprod, 0) +


nvl(i.prod.vfrete, 0) + nvl(i.prod.vseg, 0) + nvl(i.prod.voutro, 0) - nvl(i.prod.vdesc, 0)), 0)

END) aliq_calc_soma_valores,*/ --(nvl(i.prod.vprod, 0) + nvl(i.prod.vfrete, 0) +


nvl(i.prod.vseg, 0) + nvl(i.prod.voutro, 0) - nvl(i.prod.vdesc, 0)) bc_cheia_calc,

i.prod.vprod vprod,

i.prod.vfrete vfrete,

i.prod.vseg vseg,

i.prod.voutro voutro,

i.prod.vdesc vdesc,

i.imposto.icms.resumo.vbc vbc,

i.prod.vprod vicms,

i.prod.ncm ncm,

i.prod.cest cest,

i.prod.extipi extipi,

i.prod.ceantrib ceantrib,

i.prod.cean cean,

n.chave chave,

i._nitem,
c.cd_cnae cnae

FROM nfce.nfce n,

n.procnfe.nfe.infnfe.det i

JOIN usr_sat_ods.vw_ods_contrib c ON n.procnfe.nfe.infnfe.emit.cnpj = c.nu_cnpj

AND c.nm_ges = 'GESMED'

WHERE n.ano_emissao = 2023

AND n.mes_emissao = 11

AND n.situacao = 1 --Situação: 1=Autorizada; 2=Cancelada; 3=Denegada;

AND n.procnfe.nfe.infnfe.ide.tpnf = 1 --Tipo do Documento Fiscal: 0=Entrada; 1=Saída;

AND n.procnfe.nfe.infnfe.ide.finnfe = 1 --Finalidade da NF-e: 1=NF-e Normal; 2=NF-e


Complementar; 3=NF-e Ajuste;4=Devolução de mercadoria

AND n.procnfe.nfe.infnfe.emit.crt <> 1 --Código de Regime Tributário: 1=Simples Nacional;


2=Simples Nacional, excesso sublimite de receita bruta; 3=Regime Normal. (v2.0)

AND n.procnfe.nfe.infnfe.emit.enderemit.uf = 'SC'),

totais AS

(SELECT descricao,

aliq_efetiva,

cean,

ceantrib,

ncm,

cest,

extipi,

cnae,

count(1) qt_itens,

sum(vprod) vprod,

sum(vfrete) vfrete,

sum(vseg) vseg,

sum(voutro) voutro,
sum(vdesc) vdesc,

sum(vbc) vbc,

sum(vicms) vicms,

min(CONCAT(chave, '-', cast(_nitem AS string))) nfe_exemplo1,

max(CONCAT(chave, '-', cast(_nitem AS string))) nfe_exemplo2

FROM dados

GROUP BY descricao,

aliq_efetiva,

cean,

ceantrib,

ncm,

cest,

extipi,

cnae),

cruzamento AS

(SELECT t.*,

(CASE

WHEN g.gtin IS NOT NULL THEN 1

ELSE 0

END) sn_gtin_isento

FROM totais t

LEFT JOIN

(SELECT DISTINCT g.gtin

FROM teste.gesmed_isentos g

WHERE g.gtin <> '0') g ON g.gtin IN (t.cean,

t.ceantrib))

SELECT *

FROM cruzamento

VALORES TOTAIS POR DESCRIÇÃO ÚNICA


SELECT

det.item.prod.xprod AS DESCRICAO,
SUM(det.item.prod.vprod) AS total_vprod

FROM

nfce.nfce a, a.procnfe.nfe.infnfe.det det

JOIN

usr_sat_ods.vw_cad_contrib_ccicms x ON x.nu_cnpj = a.procnfe.nfe.infnfe.emit.cnpj

JOIN

teste.gessuper_aliq_inconsistente d on d.descricao = det.item.prod.xprod

where

x.nm_ges = 'GESSUPER' and a.ano_emissao = 2023 and a.mes_emissao = 10 anD x.cd_cnae


= 4711302 AND

x.nm_reg_apuracao = 'NORMAL'

GROUP BY

det.item.prod.xprod

ORDER BY total_vprod DESC

LIMPAR DESCRIÇÕES E CRUZAR DESC LIMPAS


with

gessuper_ as (

SELECT

trim(regexp_replace(

regexp_replace(

regexp_replace(

lower(d.descricao),

'\\s*[-./\\|!@&+(){}$%=,#?ºª°;<>:"0123456789]+\\s*', ' '

),

"[\\[\\]']", ""

),

'\\s{2,}', ' '

)) AS descricao_limpa,

d.*

FROM
teste.gessuper_aliq_inconsistente d

, nfce_ as (

SELECT

-- a.*,

det.imposto.icms.resumo.picms as aliq_icms,

trim(regexp_replace(

regexp_replace(

regexp_replace(

lower(det.prod.xprod),

'\\s*[-./\\|!@&+(){}$%=,#?ºª°;<>:"0123456789]+\\s*', ' '

),

"[\\[\\]']", ""

),

'\\s{2,}', ' '

)) as descricao_limpa,

sum(det.prod.vprod)

FROM nfce.nfce a, a.procnfe.nfe.infnfe.det

JOIN usr_sat_ods.vw_cad_contrib_ccicms x ON x.nu_cnpj = a.procnfe.nfe.infnfe.emit.cnpj

where

x.nm_ges = 'GESSUPER'

and a.ano_emissao = 2023

and a.mes_emissao = 10

anD x.cd_cnae = 4711302

AND x.nm_reg_apuracao = 'NORMAL'

GROUP BY det.prod.xprod, det.imposto.icms.resumo.picms

SELECT

gessuper_.*,

nfce_.*
FROM gessuper_

JOIN nfce_

ON gessuper_.descricao_limpa = nfce_.descricao_limpa

SELECIONAR ITENS ALEATÓRIOS


SELECT DISTINCT

det.item.prod.ncm AS NCM,

det.item.prod.xprod AS DESCRICAO,

a.procnfe.nfe.infnfe.emit.cnpj as cnpj,

x.cd_cnae as CNAE,

det.imposto.icms.resumo.grupotributacao,

det.imposto.icms.resumo.vicms,

det.imposto.icms.resumo.picms,

det.item.prod.cean as gtin,

x.nm_reg_apuracao

FROM

nfce.nfce a, a.procnfe.nfe.infnfe.det det

JOIN

usr_sat_ods.vw_cad_contrib_ccicms x ON x.nu_cnpj = a.procnfe.nfe.infnfe.emit.cnpj

where

x.cd_cnae = 4771701 and a.ano_emissao = 2023 and det.item.prod.ncm LIKE '300%' and

x.nm_reg_apuracao = 'NORMAL' and (det.imposto.icms.resumo.picms = 0 or


det.imposto.icms.resumo.picms is null)

and x.dt_inicio_icms <= '2022-12-31'

ORDER BY random()

LIMIT 1000000

PESQUISAR ALÍQUOTA
SELECT

SUM(det.item.prod.vprod) as TOTAL_VALOR,

det.imposto.icms.resumo.picms as ALIQ_ICMS
FROM

nfce.nfce a, a.procnfe.nfe.infnfe.det det

JOIN

usr_sat_ods.vw_cad_contrib_ccicms x ON x.nu_cnpj = a.procnfe.nfe.infnfe.emit.cnpj

where

a.ano_emissao >= 2023 and det.item.prod.xprod like "%BLOCO%" and det.item.prod.xprod


like "%PAPEL%" AND x.nm_ges = 'GESSUPER' and x.nm_reg_apuracao = "NORMAL"

GROUP BY

det.imposto.icms.resumo.picms

RELATÓRIO ANALÍTICO
SELECT

a.procnfe.nfe.infnfe.emit.xnome as NOME_EMITENTE,

a.procnfe.nfe.infnfe.emit.cnpj as CNPJ_EMITENTE,

a.procnfe.nfe.infnfe.emit.ie as IE_EMITENTE,

a.procnfe.nfe.infnfe.emit.enderemit.uf as ESTADO_EMITENTE,

a.procnfe.nfe.infnfe.dest.xnome as NOME_DESTINATARIO,

a.procnfe.nfe.infnfe.dest.cnpj as CNPJ_DESTINATARIO,

a.procnfe.nfe.infnfe.dest.im as IE_DESTINATARIO,

a.procnfe.nfe.infnfe.dest.enderDEST.uf as ESTADO_DESTINO,

det.item.prod.ncm AS NCM,

det.item.prod.xprod AS DESCRICAO,

det.item.prod.cfop AS CFOP,

det.item.prod.vprod as VALOR,

det.imposto.icms.resumo.vicms as ICMS,

det.imposto.icms.resumo.picms as ALIQ_ICMS,

a.dia_emissao as DIA,

a.mes_emissao as MES,

a.ano_emissao as ANO

FROM

nfe.nfe a, a.procnfe.nfe.infnfe.det det

JOIN
usr_sat_ods.vw_cad_contrib_ccicms x ON x.nu_cnpj = a.procnfe.nfe.infnfe.emit.cnpj

where

a.ano_emissao >= 2022 and (det.item.prod.ncm = '11010010' or (strleft(det.item.prod.ncm,


6) = '190120')) and x.nm_ges = 'GESAGRO' and a.situacao = 1 and det.item.prod.cfop > 5000
and a.procnfe.nfe.infnfe.emit.enderemit.uf ='SC'

GROUP BY

a.procnfe.nfe.infnfe.emit.xnome as NOME_EMITENTE,

a.procnfe.nfe.infnfe.emit.cnpj,

det.item.prod.ncm

RELATÓRIO SINTÉTICO
SELECT

a.procnfe.nfe.infnfe.emit.xnome as NOME_EMITENTE,

a.procnfe.nfe.infnfe.emit.ie as IE_EMITENTE,

a.procnfe.nfe.infnfe.dest.enderDEST.uf as ESTADO_DESTINO,

det.item.prod.ncm AS NCM,

det.item.prod.cfop AS CFOP,

det.item.prod.vprod as VALOR,

det.imposto.icms.resumo.picms as ALIQ_ICMS,

a.ano_emissao as ANO

FROM

nfe.nfe a, a.procnfe.nfe.infnfe.det det

JOIN

usr_sat_ods.vw_cad_contrib_ccicms x ON x.nu_cnpj = a.procnfe.nfe.infnfe.emit.cnpj

where

a.ano_emissao >= 2022 and (det.item.prod.ncm = '11010010' or (strleft(det.item.prod.ncm,


6) = '190120')) and x.nm_ges = 'GESAGRO' and a.situacao = 1 and a.procnfe.nfe.infnfe.ide.tpnf
= 1 and a.procnfe.nfe.infnfe.emit.enderemit.uf ='SC'

GROUP BY

a.ano_emissao,

a.procnfe.nfe.infnfe.emit.xnome,

a.procnfe.nfe.infnfe.emit.ie,

a.procnfe.nfe.infnfe.dest.enderDEST.uf,
det.item.prod.ncm,

det.item.prod.cfop,

det.item.prod.vprod,

det.imposto.icms.resumo.picms

Você também pode gostar