dw_pitcher_daily.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. """
  2. @desc 投手维度全量表
  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_pitcher_daily():
  12. sql="""
  13. select dt,pitcher,sum(order_amount),sum(cost),sum(reg_order_amount),sum(first_order_amount),0,0
  14. from dw_daily_channel group by dt,pitcher
  15. """
  16. data = ck.execute(sql)
  17. # db.quchen_text.execute('truncate table dw_pitcher_daily')
  18. db.quchen_text.executeMany('replace into dw_pitcher_daily values (%s,%s,%s,%s,%s,%s,%s,%s)',data)
  19. add_total()
  20. def add_total():
  21. sql='select pitcher from dw_pitcher_daily group by pitcher'
  22. pitchers= [i[0] for i in db.quchen_text.getData(sql)]
  23. sql2="""replace into dw_pitcher_daily
  24. SELECT x.dt,x.pitcher,amount,cost,reg_amount,first_reg_amount,x.total_cost,x.total_amount FROM
  25. (SELECT dt,pitcher,@total_sum:=@total_sum+reg_amount total_amount,@total_cost:=@total_cost+cost total_cost from
  26. (SELECT dt,pitcher,reg_amount,cost FROM dw_pitcher_daily where pitcher='{}' order by dt) b,(SELECT @total_sum:=0,@total_cost:=0) a
  27. ) x
  28. left join dw_pitcher_daily y on x.dt=y.dt and x.pitcher=y.pitcher"""
  29. for i in pitchers:
  30. db.quchen_text.execute(sql2.format(i))
  31. db.quchen_text.execute('delete from dw_pitcher_daily where total_cost=0')
  32. if __name__ == '__main__':
  33. dw_pitcher_daily()