Created
October 20, 2017 18:52
-
-
Save dorcilio/af5c9fbc5ea650b98c6504d8af9b9e59 to your computer and use it in GitHub Desktop.
Gist criado pelo Formatador-Planos para compartilhamento de Query SQL.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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