Advertisement
42ama

Untitled

Jan 14th, 2022
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 KB | None | 0 0
  1. with severalData
  2. as
  3. (
  4. select distinct point, date
  5. from Income
  6.  
  7. union
  8.  
  9. select distinct point, date
  10. from Outcome
  11. ),
  12. onceData as
  13. (
  14. select distinct point, date
  15. from Income_o
  16.  
  17. union
  18.  
  19. select distinct point, date
  20. from Outcome_o
  21. ),
  22. ignorePointDate as
  23. (
  24. select distinct CAST(lt.point AS NVARCHAR(255)) + CAST(lt.date AS NVARCHAR(255)) as pointDateId
  25. from onceData lt
  26. join severalData rt on lt.point = rt.point and lt.date = rt.date
  27. ),
  28. correctIncO as
  29. (
  30. select point, date, sum(inc) as inc
  31. from Income_o
  32. where CAST(point AS NVARCHAR(255)) + CAST(date AS NVARCHAR(255)) not in
  33. (select * from ignorePointDate)
  34. group by point, date
  35. ),
  36. correctOutO as
  37. (
  38. select point, date, sum(out) as out
  39. from Outcome_o
  40. where CAST(point AS NVARCHAR(255)) + CAST(date AS NVARCHAR(255)) not in
  41. (select * from ignorePointDate)
  42. group by point, date
  43. ),
  44. correctInc as
  45. (
  46. select point, date, sum(inc) as inc
  47. from Income
  48. where CAST(point AS NVARCHAR(255)) + CAST(date AS NVARCHAR(255)) not in
  49. (select * from ignorePointDate)
  50. group by point, date
  51. ),
  52. correctOut as
  53. (
  54. select point, date, sum(out) as out
  55. from Outcome
  56. where CAST(point AS NVARCHAR(255)) + CAST(date AS NVARCHAR(255)) not in
  57. (select * from ignorePointDate)
  58. group by point, date
  59. ),
  60. finalInc as
  61. (
  62. select point, date, sum(inc) as inc, tag
  63. from
  64. (
  65. select *, 'once' as tag from correctIncO union select *, 'several' as tag from correctInc
  66. ) t
  67. group by point, date, tag
  68. ),
  69. finalOut as
  70. (
  71. select point, date, sum(out) as out, tag
  72. from
  73. (
  74. select *, 'once' as tag from correctOutO union select *, 'several' as tag from correctOut
  75. ) t
  76. group by point, date, tag
  77. )
  78. select COALESCE(lt.point, rt.point),
  79. COALESCE(lt.date, rt.date),
  80. COALESCE(lt.inc, 0),
  81. COALESCE(rt.out, 0),
  82. COALESCE(lt.tag, rt.tag)
  83. from finalInc lt
  84. full join finalOut rt on lt.point = rt.point and lt.date = rt.date
  85.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement