Guest User

Untitled

a guest
Oct 20th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.24 KB | None | 0 0
  1. USE AM2012
  2. GO
  3.  
  4.  
  5. IF EXISTS (SELECT TABLE_NAME FROM INFORMATION_SCHEMA.VIEWS WHERE TABLE_NAME = 'tmpRestosAPagar')
  6. DROP VIEW tmpRestosAPagar
  7. GO
  8. CREATE VIEW tmpRestosAPagar
  9. AS
  10. SELECT cdOrgao = substring(cdFuncionalProgramatica,1,2),
  11. cdUnidade = substring(cdFuncionalProgramatica,3,3),
  12. cdFuncao = substring(cdFuncionalProgramatica,6,2),
  13. cdSubFuncao = substring(cdFuncionalProgramatica,8,3),
  14. cdPrograma = substring(cdFuncionalProgramatica,11,4),
  15. cdProjAtiv = substring(cdFuncionalProgramatica,15,4),
  16. cdFonte, flRecursoVinculado,
  17. vlProcessado, vlNaoProcessado,
  18. vlRestosAPagar,
  19. vlConfere = vlRestosAPagar - (vlProcessado + vlNaoProcessado)
  20. FROM ammRestosAPagar
  21. GO
  22.  
  23.  
  24. SELECT
  25.  
  26. CONVERT (decimal(10,2),
  27. (SELECT '000' = isnull(sum(vlRestosAPagar), 0.00)
  28. FROM tmpRestosAPagar
  29. WHERE ((cdFonte between 000 and 039) or
  30. (cdFonte between 041 and 067) or
  31. (cdFonte between 070 and 099)) and
  32. cdProjativ not between '5000' and '6999' and
  33. cdFuncao not in (10,12) )) as '000',
  34. CONVERT (decimal(10,2),
  35. ( SELECT '201' = isnull(sum(vlRestosAPagar), 0.00)
  36. FROM tmpRestosAPagar
  37. WHERE cdFonte = 000 and
  38. cdProjAtiv between '5000' and '6999' )) as '201',
  39. CONVERT (decimal(10,2),
  40. ( SELECT '202' = isnull(sum(vlRestosAPagar), 0.00)
  41. FROM tmpRestosAPagar
  42. WHERE cdFonte in (503,513,879,880) and
  43. cdProjAtiv between '5000' and '6999' )) as '202',
  44. CONVERT (decimal(10,2),
  45. ( SELECT '301' = isnull(sum(vlRestosAPagar), 0.00)
  46. FROM tmpRestosAPagar
  47. WHERE cdFuncao = 12 and
  48. cdFonte = 000 )) as '301',
  49. CONVERT (decimal(10,2),
  50. ( SELECT '302' = isnull(sum(vlRestosAPagar), 0.00)
  51. FROM tmpRestosAPagar
  52. WHERE cdFuncao = 12 and
  53. cdFonte in (103, 104) )) as '302',
  54. CONVERT (decimal(10,2),
  55. ( SELECT '303' = isnull(sum(vlRestosAPagar), 0.00)
  56. FROM tmpRestosAPagar
  57. WHERE cdFuncao = 12 and
  58. cdFonte in (101,102) )) as '303',
  59. CONVERT (decimal(10,2),
  60. ( SELECT '304' = isnull(sum(vlRestosAPagar), 0.00)
  61. FROM tmpRestosAPagar
  62. WHERE cdFuncao = 12 and
  63. ((cdFonte between 105 and 299) or
  64. (cdFonte between 671 and 685)) )) as '304',
  65. CONVERT (decimal(10,2),
  66. ( SELECT '401' = isnull(sum(vlRestosAPagar), 0.00)
  67. FROM tmpRestosAPagar
  68. WHERE cdFuncao = 10 and
  69. cdFonte = 000 )) as '401',
  70. CONVERT (decimal(10,2),
  71. ( SELECT '402' = isnull(sum(vlRestosAPagar), 0.00)
  72. FROM tmpRestosAPagar
  73. WHERE cdFuncao = 10 and
  74. cdFonte = 303 )) as '402',
  75. CONVERT (decimal(10,2),
  76. ( SELECT '403' = isnull(sum(vlRestosAPagar), 0.00)
  77. FROM tmpRestosAPagar
  78. WHERE cdFuncao = 10 and
  79. ((cdFonte between 304 and 499) or
  80. (cdFonte between 686 and 699)) )) as '403',
  81. CONVERT (decimal(10,2),
  82. ( SELECT '500' = isnull(sum(vlRestosAPagar), 0.00)
  83. FROM tmpRestosAPagar
  84. WHERE cdFuncao not in (10,12) and
  85. (cdProjAtiv not between 5000 and 6999) and
  86. ( cdFonte in (040, 068, 069, 100, 501, 502) or
  87. (cdFonte between 504 and 512) or
  88. (cdFonte between 514 and 670) or
  89. (cdFonte between 700 and 878) or
  90. (cdFonte between 881 and 998)) )) as '500'
  91.  
  92.  
  93. IF EXISTS (SELECT TABLE_NAME FROM INFORMATION_SCHEMA.VIEWS WHERE TABLE_NAME = 'tmpRestosAPagar')
  94. DROP VIEW tmpRestosAPagar
Add Comment
Please, Sign In to add comment