Skip to content

Instantly share code, notes, and snippets.

@dorcilio
Created October 20, 2017 18:52
Show Gist options
  • Save dorcilio/af5c9fbc5ea650b98c6504d8af9b9e59 to your computer and use it in GitHub Desktop.
Save dorcilio/af5c9fbc5ea650b98c6504d8af9b9e59 to your computer and use it in GitHub Desktop.
Gist criado pelo Formatador-Planos para compartilhamento de Query SQL.
SELECT
a.operacao,
SUM(a.total) AS total
FROM
(
WITH speds AS (
SELECT
a.id_reg_0000
FROM
sf.reg_0000 a
WHERE
a.dt_ini2 >= ?
AND a.dt_fin2 <= ?
AND a.id_empresa = ?
)
SELECT
b.operacao,
SUM(a.vl_opr) AS total
FROM
sf.reg_c190 a
JOIN ncm_helper.cad_cfop b ON a.cfop = b.cfop
JOIN sf.reg_0000 c ON a.id_reg_0000 = c.id_reg_0000
JOIN speds d ON a.id_reg_0000 = d.id_reg_0000
GROUP BY
b.operacao
UNION ALL
SELECT
b.operacao,
SUM(a.vl_opr) AS total
FROM
sf.reg_d190 a
JOIN ncm_helper.cad_cfop b ON a.cfop = b.cfop
JOIN sf.reg_0000 c ON a.id_reg_0000 = c.id_reg_0000
JOIN speds d ON a.id_reg_0000 = d.id_reg_0000
GROUP BY
b.operacao
UNION ALL
SELECT
b.operacao,
SUM(a.vl_opr) AS total
FROM
sf.reg_c590 a
JOIN ncm_helper.cad_cfop b ON a.cfop = b.cfop
JOIN sf.reg_0000 c ON a.id_reg_0000 = c.id_reg_0000
JOIN speds d ON a.id_reg_0000 = d.id_reg_0000
GROUP BY
b.operacao
UNION ALL
SELECT
b.operacao,
SUM(a.vl_opr) AS total
FROM
sf.reg_d590 a
JOIN ncm_helper.cad_cfop b ON a.cfop = b.cfop
JOIN sf.reg_0000 c ON a.id_reg_0000 = c.id_reg_0000
JOIN speds d ON a.id_reg_0000 = d.id_reg_0000
GROUP BY
b.operacao
UNION ALL
SELECT
b.operacao,
SUM(a.vl_opr) AS total
FROM
sf.reg_c490 a
JOIN ncm_helper.cad_cfop b ON a.cfop = b.cfop
JOIN sf.reg_0000 c ON a.id_reg_0000 = c.id_reg_0000
JOIN speds d ON a.id_reg_0000 = d.id_reg_0000
GROUP BY
b.operacao
) a
GROUP BY
a.operacao
ORDER BY
a.operacao;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment