Guest User

Untitled

a guest
Oct 21st, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.05 KB | None | 0 0
  1. SELECT
  2. a.operacao,
  3. SUM(a.total) AS total
  4. FROM
  5. (
  6. WITH speds AS (
  7. SELECT
  8. a.id_reg_0000
  9. FROM
  10. sf.reg_0000 a
  11. WHERE
  12. a.dt_ini2 >= ?
  13. AND a.dt_fin2 <= ?
  14. AND a.id_empresa = ?
  15. )
  16. SELECT
  17. b.operacao,
  18. SUM(a.vl_opr) AS total
  19. FROM
  20. sf.reg_c190 a
  21. JOIN ncm_helper.cad_cfop b ON a.cfop = b.cfop
  22. JOIN sf.reg_0000 c ON a.id_reg_0000 = c.id_reg_0000
  23. JOIN speds d ON a.id_reg_0000 = d.id_reg_0000
  24. GROUP BY
  25. b.operacao
  26. UNION ALL
  27. SELECT
  28. b.operacao,
  29. SUM(a.vl_opr) AS total
  30. FROM
  31. sf.reg_d190 a
  32. JOIN ncm_helper.cad_cfop b ON a.cfop = b.cfop
  33. JOIN sf.reg_0000 c ON a.id_reg_0000 = c.id_reg_0000
  34. JOIN speds d ON a.id_reg_0000 = d.id_reg_0000
  35. GROUP BY
  36. b.operacao
  37. UNION ALL
  38. SELECT
  39. b.operacao,
  40. SUM(a.vl_opr) AS total
  41. FROM
  42. sf.reg_c590 a
  43. JOIN ncm_helper.cad_cfop b ON a.cfop = b.cfop
  44. JOIN sf.reg_0000 c ON a.id_reg_0000 = c.id_reg_0000
  45. JOIN speds d ON a.id_reg_0000 = d.id_reg_0000
  46. GROUP BY
  47. b.operacao
  48. UNION ALL
  49. SELECT
  50. b.operacao,
  51. SUM(a.vl_opr) AS total
  52. FROM
  53. sf.reg_d590 a
  54. JOIN ncm_helper.cad_cfop b ON a.cfop = b.cfop
  55. JOIN sf.reg_0000 c ON a.id_reg_0000 = c.id_reg_0000
  56. JOIN speds d ON a.id_reg_0000 = d.id_reg_0000
  57. GROUP BY
  58. b.operacao
  59. UNION ALL
  60. SELECT
  61. b.operacao,
  62. SUM(a.vl_opr) AS total
  63. FROM
  64. sf.reg_c490 a
  65. JOIN ncm_helper.cad_cfop b ON a.cfop = b.cfop
  66. JOIN sf.reg_0000 c ON a.id_reg_0000 = c.id_reg_0000
  67. JOIN speds d ON a.id_reg_0000 = d.id_reg_0000
  68. GROUP BY
  69. b.operacao
  70. ) a
  71. GROUP BY
  72. a.operacao
  73. ORDER BY
  74. a.operacao;
Add Comment
Please, Sign In to add comment