odl_area_ad_television.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #!/usr/bin/env python
  2. #coding=utf-8
  3. """ad_television表数据处理
  4. """
  5. import sys
  6. from fty_util.common import Mysql
  7. reload(sys)
  8. sys.setdefaultencoding('utf8')
  9. """
  10. 从yxb.ad_television_tetv
  11. 提取数据插入到odl.area_ad_television 表中,作为数据分析来源数据
  12. """
  13. conn = Mysql.createOfflineConn()
  14. sql = """
  15. truncate table odl.area_ad_television
  16. """
  17. Mysql.execute(sql, conn=conn)
  18. m = 0
  19. n = 50000
  20. sql_count = """
  21. select count(id) from yxb.ad_television_tetv
  22. """
  23. count = Mysql.getOne(sql_count, conn=conn)[0]
  24. while m <= count + n:
  25. sql = """
  26. insert into odl.area_ad_television (television_id, tv_id, tv_name, epi_num, host, channel, tv_date, weekday, start_time, end_time, \
  27. theater_attribute, property, is_repeat, city, area, audience_num, audience_rating, avg_num, avg_rating, \
  28. market_rating, avg_fans, avg_view_time) \
  29. select aty.id, atl.id, aty.tv_name, aty.epi_num, aty.host, aty.channel, aty.tv_date, aty.weekday, aty.start_time, aty.end_time, aty.theater_attribute, \
  30. aty.property, aty.is_repeat, aty.city, \
  31. ary.area, ary.audience_num, ary.audience_rating, ary.avg_num, ary.avg_rating, ary.market_rating, ary.avg_fans, ary.avg_view_time \
  32. from yxb.ad_television_tetv aty \
  33. left join yxb.ad_rating_tetv ary on ary.tv_id = aty.id and ary.area like 'CSM5%%'
  34. left join yxb.ad_tv_lib atl on atl.tv_name = aty.tv_name
  35. limit %s, %s
  36. """
  37. sql = sql % (m, n)
  38. print sql
  39. Mysql.execute(sql, conn=conn)
  40. m += n
  41. sql = """
  42. update odl.area_ad_television
  43. set theater_attribute =
  44. (
  45. case
  46. when start_time >= '00:00:00' and end_time <= '6:00:00' then '凌晨剧场'
  47. when start_time >= '7:00:00' and end_time < '9:00:00' then '早间剧场'
  48. when start_time >= '9:00:00' and end_time <= '12:00:00' then '上午剧场'
  49. when start_time >= '14:00:00' and end_time < '18:00:00' then '下午剧场'
  50. when start_time >= '19:30:00' and end_time <= '21:30:00' then '黄金剧场'
  51. when (start_time >= '18:00:00' and end_time < '19:30:00') or (start_time > '21:30:00' and end_time < '24:00:00') then '晚间剧场'
  52. end
  53. )
  54. where theater_attribute is null or theater_attribute = ''
  55. """
  56. Mysql.execute(sql, conn=conn)
  57. Mysql.close(conn)