Advertisement
Guest User

Untitled

a guest
Sep 19th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.43 KB | None | 0 0
  1. with
  2. wt as -- смены
  3. (
  4. select
  5. resource_id,
  6. resource_type,
  7. trunc(time_start) as shift_date,
  8. round((cast(min(time_end) as date) - cast(max(time_start) as date)) * 24 * 60) as minutes
  9. from
  10. resource_schedule
  11. where
  12. resource_type='T'
  13. and event_type='A'
  14. and trunc(time_start) between cast(trunc(sysdate) as date)-3 and cast(trunc(sysdate) as date)+4
  15. group by
  16. resource_id,
  17. resource_type,
  18. trunc(time_start)
  19. having
  20. cast(min(time_end) as date) - cast(max(time_start) as date) > 0
  21. ),
  22. bk as -- букинги
  23. (
  24. select
  25. br.resource_type,
  26. br.resource_id,
  27. trunc(b.time_start) as shift_date,
  28. round(sum(cast(b.time_end as date) - cast(b.time_start as date)) * 24 * 60) as minutes,
  29. mi.fgrp,
  30. count(mi.fgrp) as count
  31. from
  32. bookings b,
  33. booking_resources br,
  34. menu_items mi
  35. where
  36. b.booking_id = br.booking_id
  37. and trunc(b.time_start) between cast(trunc(sysdate) as date)-3 and cast(trunc(sysdate) as date)+4
  38. and b.status != 'V'
  39. and br.resource_type in ('T', 'F')
  40. and mi.mi_id=b.mi_id
  41. group by
  42. br.resource_type,
  43. br.resource_id,
  44. trunc(b.time_start),
  45. mi.fgrp
  46. having
  47. sum(cast(b.time_end as date) - cast(b.time_start as date)) > 0
  48. ),
  49. chk as -- чеки
  50. (
  51. select
  52. trunc(dd.tm) shift_date,
  53. dd.comission_empl_id,
  54. sum(dd.ttl - dd.dsc) ttl,
  55. count(distinct d.def_card_id) guests
  56. from
  57. docs d, docs_dtl dd
  58. where
  59. dd.doc_id = d.doc_id and dd.tm between cast(trunc(sysdate) as date)-3 and cast(trunc(sysdate) as date)+4
  60. group by
  61. trunc(dd.tm),
  62. dd.comission_empl_id
  63. )
  64. select
  65. nvl(wt.shift_date, bk.shift_date) as shift_date,
  66. nvl(wt.resource_type, bk.resource_type) as resource_type,
  67. nvl(wt.resource_id, bk.resource_id) as resource_id,
  68. case
  69. when nvl(wt.resource_type, bk.resource_type) = 'T' then
  70. (select name2 || ' ' || name1 from employees where empl_id = nvl(wt.resource_id, bk.resource_id))
  71. when nvl(wt.resource_type, bk.resource_type) = 'F' then
  72. (select name from facilities where facilities_id = nvl(wt.resource_id, bk.resource_id))
  73. end as resource_name,
  74. case
  75. when nvl(wt.resource_type, bk.resource_type) = 'T' then 'Сотрудники'
  76. when nvl(wt.resource_type, bk.resource_type) = 'F' then 'Помещения'
  77. end as resource_type_text,
  78. round(nvl(bk.minutes, 0) / nvl(wt.minutes, case when bk.resource_type = 'T' then bk.minutes else 12 * 60 end) * 1000 ) /10 as utilization,
  79. case
  80. when nvl(wt.resource_type, bk.resource_type) = 'T' then
  81. nvl(round(chk.ttl/chk.guests * 100)/100, 0)
  82. else 0 end check_avg,
  83. case
  84. when nvl(wt.resource_type, bk.resource_type) = 'T' then
  85. nvl(chk.guests, 0)
  86. else 0 end check_cnt,
  87. to_char(nvl(wt.shift_date, bk.shift_date), 'DD.MM.YY') as shift_date_text,
  88. case when nvl(wt.shift_date, bk.shift_date) > cast(trunc(sysdate) as date)
  89. then nvl(wt.shift_date, bk.shift_date) - cast(trunc(sysdate) as date)
  90. else 0 end as shift_num
  91. ,bk.fgrp, bk.count
  92.  
  93. from
  94. wt full outer join bk
  95. on wt.resource_id = bk.resource_id and wt.resource_type = bk.resource_type
  96. and wt.shift_date = bk.shift_date
  97. left join chk
  98. on (chk.comission_empl_id = nvl(wt.resource_id, bk.resource_id) and chk.shift_date=nvl(wt.shift_date, bk.shift_date))
  99. where bk.resource_type='T'
  100. --and bk.shift_date=trunc(sysdate)
  101. order by 1,2,4
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement