Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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;
Add Comment
Please, Sign In to add comment