dw_channel_daily.py 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. """
  2. @desc 号维度全量表 ck上跑完dw_daily_channel 并同步到mysql dw_channel_daily
  3. @auth ck
  4. """
  5. import time
  6. from model.DateUtils import DateUtils
  7. from model.DataBaseUtils import MysqlUtils,CkUtils
  8. du = DateUtils()
  9. db= MysqlUtils()
  10. ck = CkUtils()
  11. def dw_daily_channel():
  12. sql="""insert into dw_daily_channel
  13. select
  14. dt,channel,pitcher,stage,platform,book,
  15. order_count,order_user,order_amount,first_order_count,first_order_user,first_order_amount,
  16. view_count,click_count,follow_user,cost,reg_order_count,reg_order_user,
  17. reg_order_amount,reg_order_amount30,web_view_count,platform_view_count,web_order_count,total_cost,
  18. total_amount,reg_order_user_again,reg_order_user7,reg_order_user30,reg_order_amount7,type,
  19. total_first_amount,require_roi,require_mult
  20. from
  21. (select dt,channel, pitcher,stage,platform,book,cost,view_count,click_count,follow_user,web_view_count,platform_view_count,web_order_count,type,require_roi,require_mult from dw_daily_channel_cost) a
  22. left outer join
  23. (select toDate(formatDateTime(reg_time,'%Y-%m-%d')) as dt2,channel as channel2,
  24. sum(amount) as reg_order_amount,
  25. count(distinct user_id) as reg_order_user,
  26. count(1) as reg_order_count,
  27. sum(if(subtractDays(date, 30)>reg_time,0,amount)) as reg_order_amount30,
  28. count(distinct if(subtractDays(date, 7)>reg_time,'',user_id))-1 reg_order_user7,
  29. sum(if(subtractDays(date, 7)>reg_time,0,amount)) as reg_order_amount7,
  30. count(distinct if(subtractDays(date, 30)>reg_time,'',user_id))-1 reg_order_user30
  31. from order where reg_time>'2019-03-18 00:00:00' group by toDate(formatDateTime(reg_time,'%Y-%m-%d')),channel) b
  32. on dt=dt2 and channel=channel2 left outer join
  33. (select date as dt3,channel as channel3,
  34. count(1) as first_order_count,
  35. count(distinct user_id) as first_order_user,
  36. sum(amount) as first_order_amount
  37. from order where toDate(reg_time)=date group by date,channel) c
  38. on dt=dt3 and channel=channel3
  39. left outer join
  40. (select date as dt4,channel as channel4,
  41. count(1) as order_count,
  42. count(distinct user_id) as order_user,
  43. sum(amount) as order_amount
  44. from order group by date,channel) d
  45. on dt=dt4 and channel=channel4
  46. left outer join
  47. (select dt dt5,channel channel5,total_cost,total_amount,total_first_amount from dw_channel_daily_total ) e
  48. on dt=dt5 and channel=channel5
  49. left outer join (
  50. select sum(if(user_order_count>1,1,0)) reg_order_user_again,channel channel6,toDate(reg_date) dt6 from (
  51. select formatDateTime(reg_time,'%Y-%m-%d') reg_date,channel,count(1) user_order_count
  52. from order group by formatDateTime(reg_time,'%Y-%m-%d') ,user_id,channel
  53. ) x group by reg_date,channel
  54. ) f on dt=dt6 and channel=channel6
  55. having order_amount+cost+reg_order_amount>0
  56. """
  57. ck.execute("truncate table dw_daily_channel")
  58. ck.execute(sql)
  59. print("ok")
  60. data=ck.execute('select * from dw_daily_channel')
  61. insert_sql="insert into dw_channel_daily values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)"
  62. db.dm.execute("truncate table dw_channel_daily")
  63. db.dm.executeMany(insert_sql,data)
  64. db.quchen_text.execute("truncate table dw_channel_daily")
  65. db.quchen_text.executeMany(insert_sql,data)
  66. if __name__ == '__main__':
  67. dw_daily_channel()