Advertisement
Guest User

Untitled

a guest
Mar 26th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.04 KB | None | 0 0
  1. select
  2. monday as dt
  3. ,bonus_id
  4. ,title as "any(title)"
  5. ,case
  6. when title like '%Welcome%' then 'welcome'
  7. else 'other'
  8. end
  9. ,round(sumIf(payment,paid_status = 1)) as real_pay
  10. ,round(sum(payment)) as MAX_pay
  11. ,sumIf(period_order_count,paid_status = 1) as sum_bonus_order
  12. ,sumIf(bonus_count,paid_status = 1) as sum_all_ident_order
  13. ,round(sumIf(payment,paid_status = 1)/sumIf(period_order_count,paid_status = 1)) as AVG_bonus_order
  14. ,round(sumIf(payment, paid_status = 1)/sumIf(bonus_count,bonus_count = 1),2) as AVG_all_ident_order
  15. ,round(sum(if(paid_status = 1, payment, 0))/sum(payment/100),2) as share_of_max
  16. ,round(countIf(paid_status = 1)/count(*),2) as share_idents_of_participant
  17. ,round(sum(all_ident_order_sum)/sum(all_ident_order_cnt),2) as avg_ident_bill
  18. ,round((sum(if(paid_status = 1, payment/100, 0))/sum(if(paid_status = 1, bonus_count, null)))/(sum(all_ident_order_sum)/sum(all_ident_order_cnt)),2) as share_of_avg_ident_bill
  19. ,week_orders.week_order_cnt
  20. ,round(sum(if(paid_status = 1, payment/100, 0)))/week_orders.week_order_cnt as pay_per_1_order_of_week
  21. from (
  22. SELECT
  23. ident_id, bonus_id, bonus_count, payment, period_start_date, period_end_date, title, monday,paid_status,period_order_count
  24. ,sum(all_ident_sum) as all_ident_order_sum
  25. ,sum(cnt) as all_ident_order_cnt
  26. from (
  27. SELECT OrderedDate, ident_id, sum(ident_bill) as all_ident_sum, count(*) as cnt
  28. from (
  29. SELECT ident as ident_id, ident_bill, id, toDate(OrderedDate) as OrderedDate
  30. FROM etl_import.order_closed_dist
  31. ALL LEFT JOIN etl_import.order_closed_info_dist ocid using id
  32. where status = 'CP' and companyid = 1 and g_type = 0
  33. and (autoget = 3 or current_autoget = 3)
  34. and toDate(OrderedDate) >= toDate(now()) - interval 6 week
  35. )
  36. where ident_id in (
  37. SELECT distinct
  38. toUInt64(ident_id) as ident_id
  39. from mysql()
  40. )
  41. group by ident_id, OrderedDate
  42. order by OrderedDate
  43. ) as cnt
  44. all inner join
  45. (
  46. SELECT bonus_id, paid_status, payment/100 as payment, bonus_count, title,period_order_count
  47. ,toDate(ifNull(period_date_end_dboc, period_date_end)) as period_end_date
  48. ,toDate(period_end_date - INTERVAL period day) as period_start_date
  49. ,toMonday(period_end_date) as monday
  50. ,ident_id
  51. from
  52. (
  53. SELECT
  54. toUInt64(ident_id) as ident_id, period_date_start as period_date_start_dboc, period_date_end as period_date_end_dboc, paid_status, bonus_id, count as bonus_count
  55. from mysql()
  56. )
  57. all inner join (
  58. select toUInt64(ident_id) as ident_id, bonus_id, title, payment, period_order_count, period_date_start, period_date_end, period
  59. from mysql()
  60. any inner join (select id as bonus_id, period, title from mysql())
  61. using bonus_id
  62. ) as dbs
  63. using ident_id, bonus_id
  64. where period_end_date > toDate(now()) - interval 6 week
  65. )
  66. using ident_id
  67. where period_end_date < toMonday(now()) and OrderedDate between period_start_date and (period_end_date - INTERVAL 1 day)
  68. group by ident_id, bonus_id, bonus_count, payment, period_start_date, period_end_date, title, monday,paid_status,period_order_count
  69. )
  70. all left join (select count(*) as week_order_cnt, monday from etl_import.order_closed_dist
  71. where toDate(OrderedDate) > toDate(now()) - interval 7 week and status = 'CP' and id_client > 0 and companyid = 1 and g_type = 0
  72. group by toMonday(OrderedDate) as monday
  73. ) as week_orders using monday
  74. where monday <= toDate(now()) and toDate(monday) >toDate(now()) - interval 7 week
  75. group by bonus_id, monday, week_order_cnt,title
  76. order by dt, bonus_id;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement