Guest User

Untitled

a guest
May 27th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.33 KB | None | 0 0
  1. select c.id company_id, c.name company, DATE(receipt_date) receipt, count(*) count from act
  2. join company c on c.id = act.company_id
  3. where
  4.  
  5. act.is_delete = 0
  6. group by act.company_id, receipt
  7.  
  8. select c.id company_id, c.name company, DATE(issue_date) issue, count(*) count from act
  9. join company c on c.id = act.company_id
  10. where
  11.  
  12. act.is_delete = 0
  13. group by act.company_id, issue
  14.  
  15. select company_id,
  16. date(receipt_date) as "dt",
  17. count(*) as "qty"
  18. into #receipt_total
  19. from act
  20. where is_delete = 0
  21. group by company_id,
  22. "dt"
  23. ;
  24. -- indexes
  25.  
  26. select company_id,
  27. date(receipt_date) as "dt",
  28. count(*) as "qty"
  29. into #issue_total
  30. from act
  31. where is_delete = 0
  32. group by company_id,
  33. "dt"
  34. ;
  35. -- indexes
  36.  
  37.  
  38. select isnull(t1.company_id, t2.company_id) as "company_id",
  39. isnull(t1.dt, t2.dt) as "dt",
  40. isnull(t1.qty, 0) as "qty_receipt",
  41. isnull(t2.qty, 0) as "qty_issue"
  42. into #result
  43. from #receipt_total as t1
  44. full join #issue_total as t2 on t2.company_id = t1.company_id
  45. and t2.dt = t1.dt
  46. ;
  47.  
  48. select t1.company_id,
  49. t2.company_name,
  50. t1.dt,
  51. t1.qty_receipt,
  52. t1.qty_issue
  53. from #result as t1
  54. join company as t2 on t2.id = t1.company_id
  55. ;
  56.  
  57. select company_id,
  58. 'receipt' as "rtype",
  59. date(receipt_date) as "dt",
  60. count(*) as "qty"
  61. into #receipt_total
  62. from act
  63. where is_delete = 0
  64. group by company_id,
  65. "dt"
  66. ;
  67. -- indexes
  68.  
  69. select company_id,
  70. 'issue' as "rtype",
  71. date(receipt_date) as "dt",
  72. count(*) as "qty"
  73. into #issue_total
  74. from act
  75. where is_delete = 0
  76. group by company_id,
  77. "dt"
  78. ;
  79. -- indexes
  80.  
  81. select *
  82. into #total_data
  83. from #receipt_total
  84. union all
  85. select *
  86. from #issue_total
  87. ;
  88.  
  89. select company_id,
  90. dt,
  91. sum(case when rtype = 'receipt'
  92. then qty
  93. end) as "qty_receipt",
  94. sum(case when rtype = 'issue'
  95. then qty
  96. end) as "qty_issue"
  97. into #result
  98. from #total_data
  99. ;
  100.  
  101. select t1.company_id,
  102. t2.company_name,
  103. t1.dt,
  104. t1.qty_receipt,
  105. t1.qty_issue
  106. from #result as t1
  107. join company as t2 on t2.id = t1.company_id
  108. ;
Add Comment
Please, Sign In to add comment