odl_ad_television_incr_update.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #!/usr/bin/env python
  2. #coding=utf-8
  3. """ad_television表数据增量更新
  4. """
  5. import datetime
  6. import sys
  7. from fty_util.common import Mysql
  8. reload(sys)
  9. sys.setdefaultencoding('utf8')
  10. conn = Mysql.createOfflineConn()
  11. sql = """
  12. select max(tv_date) as max_date from odl.ad_television
  13. """
  14. row = Mysql.getOne(sql, conn=conn)
  15. max_date = row[0]
  16. # year = datetime.datetime.strptime(max_date, '%Y-%m-%d').year
  17. sql = """
  18. insert into odl.ad_television (television_id, tv_id, tv_name, epi_num, host, channel, tv_date, weekday, start_time, end_time, \
  19. theater_attribute, property, is_repeat, city, year, area, audience_num, audience_rating, avg_num, avg_rating, \
  20. market_rating, avg_fans, avg_view_time) \
  21. 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, \
  22. aty.property, aty.is_repeat, aty.city, substring_index(aty.tv_date, '-', 1), \
  23. ary.area, ary.audience_num, ary.audience_rating, ary.avg_num, ary.avg_rating, ary.market_rating, ary.avg_fans, ary.avg_view_time \
  24. from yxb.ad_television_2016 aty \
  25. left join yxb.ad_rating_2016 ary on ary.tv_id = aty.id and ary.area like 'CSM5%%'
  26. left join yxb.ad_tv_lib atl on atl.tv_name = aty.tv_name
  27. where aty.tv_date > '%s'
  28. """
  29. sql = sql % (max_date)
  30. Mysql.execute(sql, conn=conn)
  31. """
  32. 凌晨剧场 0:00 - 6:00
  33. 早间剧场:7:00-9:00
  34. 上午剧场 9:00-12:00
  35. 下午剧场 14:00-18:00
  36. 晚间剧场 18:00-24:00
  37. 黄金剧场:19:30-21:30
  38. """
  39. sql = """
  40. update odl.ad_television
  41. set theater_attribute =
  42. (
  43. case
  44. when start_time >= '00:00:00' and end_time <= '6:00:00' then '凌晨剧场'
  45. when start_time >= '7:00:00' and end_time < '9:00:00' then '早间剧场'
  46. when start_time >= '9:00:00' and end_time <= '12:00:00' then '上午剧场'
  47. when start_time >= '14:00:00' and end_time < '18:00:00' then '下午剧场'
  48. when start_time >= '19:30:00' and end_time <= '21:30:00' then '黄金剧场'
  49. 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 '晚间剧场'
  50. end
  51. )
  52. where tv_date > '%s' and (theater_attribute is null or theater_attribute = '')
  53. """
  54. sql = sql % (max_date)
  55. Mysql.execute(sql, conn=conn)
  56. Mysql.close(conn)