sql.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. class Sql:
  2. # 获取项目的城市信息
  3. sql_1 = """
  4. select IFNULL(brand_id, -1), house_id, house_name, city from d_house a where brand_id = '13' and house_id != '13' and status = 1 order by house_id
  5. """
  6. # 根据任务id获取推送客户信息
  7. sql_2 = """
  8. select a.task_key, b.customer_type, b.name, b.mail, b.house_or_region, a.customer_id, GROUP_CONCAT(DISTINCT c.house_or_brand_id) as ids
  9. from report_task_info a left join report_push_customer_info b on b.id = a.customer_id
  10. left join report_customer_authority_info c on b.id = c.customer_id
  11. LEFT JOIN d_house d on d.house_id = c.house_or_brand_id or d.brand_id = c.house_or_brand_id
  12. where a.task_key = %s and a.status = b.status = c.status = 1 and d.`status` = 1
  13. group by a.task_key, b.customer_type, b.name, b.mail, b.house_or_region, a.customer_id
  14. """
  15. # 1:总浏览量
  16. #
  17. # 集团部分+项目部分!!!
  18. # ----项目PV--权限项目范围内,求和
  19. # 参数:时间区间 和 项目列表
  20. sql_3 = """
  21. SELECT SUM(pv) AS pv1 FROM a_idfa_behavior_sum
  22. WHERE report_d >= %s and report_d <= %s AND house_id IN %s
  23. """
  24. # ----集团PV--权限项目范围内,求和
  25. # 参数:数据区间和项目列表
  26. sql_4 = """
  27. SELECT SUM(pv) AS pav2 FROM a_behavior_brand_mini_day
  28. WHERE report_d >= %s and report_d <= %s AND house_id IN %s
  29. """
  30. # 2:总浏览人数
  31. #
  32. # 参数,数据区间, 项目列表
  33. sql_5 = """
  34. SELECT
  35. count(
  36. DISTINCT IFNULL(mobile, user_id)
  37. ) as people
  38. FROM
  39. (
  40. SELECT
  41. A.user_id,
  42. B.mobile
  43. FROM
  44. a_idfa_behavior_sum A
  45. LEFT JOIN d_user B ON A.user_id = B.user_id
  46. WHERE
  47. A.report_d >= %s and A.report_d <= %s
  48. AND A.house_id IN %s
  49. UNION
  50. SELECT
  51. A.brand_user_id AS user_id,
  52. B.mobile
  53. FROM
  54. a_behavior_brand_mini_day A
  55. LEFT JOIN a_brand_app_customer B ON A.brand_user_id = B.brand_customer_id
  56. WHERE
  57. A.report_d >= %s and A.report_d <= %s
  58. AND A.house_id IN %s
  59. ) t1
  60. """
  61. # 3:新增获客
  62. sql_6 = """
  63. SELECT
  64. count(
  65. DISTINCT IFNULL(mobile, user_id)
  66. )
  67. FROM
  68. (
  69. SELECT
  70. user_id,
  71. mobile,
  72. created
  73. FROM
  74. d_user
  75. WHERE
  76. created >= %s
  77. AND created <= %s
  78. AND house_id IN %s
  79. AND source ='3'
  80. UNION
  81. SELECT
  82. brand_customer_id AS user_id,
  83. mobile,
  84. rlat_created
  85. FROM
  86. a_brand_app_customer_house_rlat
  87. WHERE
  88. rlat_created >= %s
  89. AND rlat_created <= %s
  90. AND rlat_house_id IN %s
  91. AND cust_house_flag ='1'
  92. ) t1
  93. """
  94. # 4:新增获电
  95. sql_7 = """
  96. SELECT
  97. COUNT(DISTINCT mobile)
  98. FROM
  99. (
  100. SELECT
  101. user_id,
  102. mobile,
  103. wx_phone_time AS created
  104. FROM
  105. d_user
  106. WHERE
  107. wx_phone_time >= %s
  108. AND wx_phone_time <= %s
  109. AND house_id IN %s
  110. and source = '3'
  111. UNION
  112. SELECT
  113. brand_customer_id,
  114. mobile,
  115. houdian_time AS created
  116. FROM
  117. (
  118. SELECT
  119. *,
  120. CASE
  121. WHEN rlat_created > shouquan_time THEN
  122. rlat_created
  123. ELSE
  124. shouquan_time
  125. END AS houdian_time
  126. FROM
  127. a_brand_app_customer_house_rlat
  128. WHERE
  129. mobile IS NOT NULL
  130. AND rlat_house_id IN %s
  131. AND cust_house_flag = '1'
  132. ) t1
  133. WHERE
  134. houdian_time >= %s
  135. AND houdian_time <= %s
  136. ) t1
  137. """
  138. # hosue_id, type, count
  139. sql_8 = """
  140. select house_id, type, COUNT(DISTINCT customer_mobile) from f_dm_jianye_allagent_day where report_d >= %s and report_d <= %s group by house_id, type
  141. """
  142. sql_9 = """
  143. SELECT house_id, visit_new_uv_dtd FROM a_brand_customer_share_dtd where brand_id ='13' and report_d >= %s and report_d <= %s order by report_d
  144. """
  145. sql_10 = """
  146. select type, COUNT(DISTINCT customer_mobile) from f_dm_jianye_allagent_day where report_d >= %s and report_d <= %s group by type
  147. """
  148. sql_11 = """"
  149. select a.house_or_region, min(b.house_or_brand_id), sum(a.name) from report_push_customer_info a left join report_customer_authority_info b on a.id = b.customer_id group by house_or_region
  150. """
  151. sql_12 = """
  152. insert into report_push_log(name, mail, report_name, push_time, send_status, status, error_message) values(%s, %s, %s, now(), %s, 1, %s)
  153. """
  154. sql_13 = """
  155. SELECT
  156. x.brand_id,
  157. x.house_id,
  158. x.house_name,
  159. ifnull(x.pv, 0),
  160. x.uv,
  161. x.new_cust,
  162. x.shouquan_cust
  163. FROM
  164. (
  165. SELECT
  166. c.pv,
  167. c.uv,
  168. a.brand_id,
  169. a.house_id,
  170. a.house_name,
  171. a.brand_name,
  172. ifnull(b.house_layout_num, 0) house_layout_num,
  173. ifnull(d.launch_time, '--') launch_time,
  174. c.new_cust,
  175. c.shouquan_cust,
  176. c.revisit_cust
  177. FROM
  178. (
  179. SELECT
  180. brand_id,
  181. ifnull(house_id, '0') house_id,
  182. sum(pv) pv,
  183. count(DISTINCT brand_user_id) uv,
  184. count(
  185. DISTINCT CASE
  186. WHEN is_new_user = 1 THEN
  187. brand_user_id
  188. END
  189. ) new_cust,
  190. count(
  191. DISTINCT CASE
  192. WHEN is_shouquan_user = 1 THEN
  193. brand_user_id
  194. END
  195. ) shouquan_cust,
  196. count(
  197. DISTINCT CASE
  198. WHEN is_new_user = 0 THEN
  199. brand_user_id
  200. END
  201. ) revisit_cust
  202. FROM
  203. a_behavior_brand_mini_day
  204. WHERE
  205. report_d >= %s
  206. AND report_d <= %s
  207. GROUP BY
  208. brand_id,
  209. ifnull(house_id, '0')
  210. ) c
  211. LEFT JOIN (
  212. SELECT
  213. house_id,
  214. count(1) house_layout_num
  215. FROM
  216. d_content_layout
  217. WHERE
  218. `status` = '1'
  219. AND house_id <> 1
  220. GROUP BY
  221. house_id
  222. UNION ALL
  223. SELECT
  224. bb.brand_id house_id,
  225. count(1) house_layout_num
  226. FROM
  227. d_content_layout aa
  228. JOIN d_house bb ON aa.house_id = bb.house_id
  229. WHERE
  230. aa.`status` = '1'
  231. AND bb. STATUS = '1'
  232. AND aa.house_id <> 1
  233. GROUP BY
  234. bb.brand_id
  235. ) b ON c.house_id = b.house_id
  236. JOIN d_house a ON a.house_id = c.house_id
  237. AND a.brand_id = c.brand_id
  238. LEFT JOIN d_house_attr d ON c.house_id = d.house_id
  239. AND c.brand_id = d.brand_id
  240. ) x
  241. """
  242. sql_14 = """
  243. select a.id , a.`name`, a.mail, a.customer_type, a.house_or_region, GROUP_CONCAT(b.house_or_brand_id)
  244. from report_push_customer_info a
  245. left join report_customer_authority_info b on a.id = b.customer_id
  246. where a.id = %s and a.status = 1 and b.status = 1
  247. group by a.id , a.`name`, a.mail, a.customer_type, a.house_or_region
  248. """
  249. sql_15 = """
  250. select * from d_jianye_house_city_region_rlat
  251. """
  252. sql_16 = """
  253. SELECT b.region, b.city, a.house_name, a.advi_name, a.mobile, a.org_name, IFNULL(a.fx_mobile,0), IFNULL(a.zc_mobile, 0), IFNULL(a.bb_mobile, 0) FROM f_dm_jianye_agent_house_day a left join d_jianye_house_city_region_rlat b on a.house_id = b.house_id where a.report_d = %s and a.house_id in %s order by a.org_name, CAST(a.fx_mobile AS SIGNED) desc
  254. """
  255. sql_17 = """
  256. SELECT b.region, b.city, a.house_name, a.advi_name, a.mobile, a.org_name, IFNULL(a.fx_mobile,0), IFNULL(a.zc_mobile, 0), IFNULL(a.bb_mobile, 0) FROM f_dm_jianye_agent_house_month a left join d_jianye_house_city_region_rlat b on a.house_id = b.house_id where a.report_d = %s and a.house_id in %s order by a.org_name, CAST(a.fx_mobile AS SIGNED) desc
  257. """
  258. sql_18 = """
  259. SELECT b.region, b.city, a.house_name, a.advi_name, a.mobile, a.org_name, IFNULL(a.fx_mobile,0), IFNULL(a.zc_mobile, 0), IFNULL(a.bb_mobile, 0) FROM f_dm_jianye_agent_house_all a left join d_jianye_house_city_region_rlat b on a.house_id = b.house_id where a.report_d = %s and a.house_id in %s order by a.org_name, CAST(a.fx_mobile AS SIGNED) desc
  260. """
  261. # 项目
  262. sql_19 = """
  263. SELECT house_id
  264. ,COUNT(DISTINCT mobile) AS new_moblie_qutty
  265. ,sum(CASE WHEN adviser_id IS NULL THEN 1 END ) AS Dispatchable_mobile_qutty
  266. ,COUNT(
  267. DISTINCT CASE WHEN adviser_id IS NOT NULL THEN mobile
  268. END
  269. ) AS Dispatched_mobile_qutty
  270. FROM gdm_m01_cus_mobile_total
  271. WHERE register_time >= %s
  272. AND register_time <= %s
  273. and house_id in %s and brand_id = 13
  274. group by house_id
  275. """
  276. #集团
  277. sql_20 = """
  278. SELECT brand_id
  279. ,COUNT(DISTINCT mobile) AS new_moblie_qutty
  280. ,sum(CASE WHEN adviser_id IS NULL THEN 1 END ) AS Dispatchable_mobile_qutty
  281. ,COUNT(
  282. DISTINCT CASE WHEN adviser_id IS NOT NULL THEN mobile
  283. END
  284. ) AS Dispatched_mobile_qutty
  285. FROM gdm_m01_cus_mobile_total
  286. WHERE register_time >= %s
  287. AND register_time <= %s
  288. and brand_id = 13
  289. group by brand_id
  290. """
  291. if __name__ == '__main__':
  292. print(len('222'))