dataworks_single_user_180d_all_in_one.sql 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. -- DataWorks节点参数示例:
  2. -- mid=weixin_openid_xxx
  3. -- anchor_dt=20260810
  4. -- window_start_dt=20260211
  5. -- window_end_dt=20260809
  6. -- 窗口为锚点前180个自然日,不包含锚点当天。
  7. -- 注意:各指标还受源表实际分区保留期限制。2026-08-14检查时:
  8. -- ods_video_play_log_day和user_share_log可完整覆盖示例180天窗口;
  9. -- video_action_log_applet最早分区为20260416,设备/地域及部分风险排除条件覆盖116天;
  10. -- simpleevent_log最早分区为20260615,截图及切后台相关指标只能覆盖56天。
  11. -- 其余本SQL使用的表可完整覆盖示例180天窗口,详见fixtures下的分区快照。
  12. WITH input_user AS (
  13. SELECT
  14. '${mid}' AS mid,
  15. '${anchor_dt}' AS anchor_dt,
  16. '${window_start_dt}' AS window_start_dt,
  17. '${window_end_dt}' AS window_end_dt
  18. ),
  19. active_metrics AS (
  20. SELECT
  21. i.mid,
  22. COUNT(DISTINCT a.dt) AS active_day_cnt,
  23. CONCAT_WS(',', COLLECT_SET(CASE
  24. WHEN a.opengid IS NOT NULL AND a.opengid <> '' THEN a.opengid
  25. END)) AS source_opengid_list
  26. FROM input_user i
  27. LEFT JOIN loghubods.useractive_log a
  28. ON a.machinecode = i.mid
  29. AND a.dt BETWEEN '${window_start_dt}' AND '${window_end_dt}'
  30. AND a.businesstype = 'path'
  31. GROUP BY i.mid
  32. ),
  33. real_play_metrics AS (
  34. SELECT i.mid, COUNT(p.mid) AS real_play_cnt
  35. FROM input_user i
  36. LEFT JOIN loghubods.ods_video_play_log_day p
  37. ON p.mid = i.mid
  38. AND p.dt BETWEEN '${window_start_dt}' AND '${window_end_dt}'
  39. AND p.businesstype = 'videoRealPlay'
  40. GROUP BY i.mid
  41. ),
  42. share_metrics AS (
  43. SELECT i.mid, COUNT(s.machinecode) AS video_share_cnt
  44. FROM input_user i
  45. LEFT JOIN loghubods.user_share_log s
  46. ON s.machinecode = i.mid
  47. AND s.dt BETWEEN '${window_start_dt}' AND '${window_end_dt}'
  48. AND s.topic = 'share'
  49. GROUP BY i.mid
  50. ),
  51. click_events AS (
  52. SELECT i.mid, s.clickobjectid, s.hotsencetype, s.shareid
  53. FROM input_user i
  54. JOIN loghubods.user_share_log s
  55. ON s.machinecode = i.mid
  56. AND s.dt BETWEEN '${window_start_dt}' AND '${window_end_dt}'
  57. AND s.topic = 'click'
  58. ),
  59. click_metrics AS (
  60. SELECT
  61. mid,
  62. COUNT(DISTINCT CASE WHEN clickobjectid IS NOT NULL AND clickobjectid <> ''
  63. THEN clickobjectid END) AS card_click_object_uv,
  64. SUM(CASE WHEN clickobjectid IS NOT NULL AND clickobjectid <> ''
  65. THEN 1 ELSE 0 END) AS card_click_object_pv,
  66. SUM(CASE WHEN hotsencetype = '1007' THEN 1 ELSE 0 END) AS source_1007_cnt,
  67. SUM(CASE WHEN hotsencetype = '1008' THEN 1 ELSE 0 END) AS source_1008_cnt,
  68. SUM(CASE WHEN hotsencetype = '1058' THEN 1 ELSE 0 END) AS source_1058_cnt,
  69. SUM(CASE WHEN hotsencetype = '1074' THEN 1 ELSE 0 END) AS source_1074_cnt,
  70. SUM(CASE WHEN hotsencetype IN ('1067', '1095') THEN 1 ELSE 0 END)
  71. AS source_1067_1095_cnt,
  72. CONCAT_WS(',', COLLECT_SET(CASE WHEN shareid IS NOT NULL AND shareid <> ''
  73. THEN shareid END)) AS source_shareid_list
  74. FROM click_events
  75. GROUP BY mid
  76. ),
  77. other_scene_counts AS (
  78. SELECT mid, hotsencetype, COUNT(*) AS scene_cnt
  79. FROM click_events
  80. WHERE hotsencetype IS NOT NULL AND hotsencetype <> ''
  81. AND hotsencetype NOT IN ('1007','1008','1058','1074','1067','1095')
  82. GROUP BY mid, hotsencetype
  83. ),
  84. other_scene_metrics AS (
  85. SELECT
  86. mid,
  87. CONCAT_WS(',', COLLECT_SET(CONCAT(hotsencetype, ':', CAST(scene_cnt AS STRING))))
  88. AS other_scene_list
  89. FROM other_scene_counts
  90. GROUP BY mid
  91. ),
  92. open_conv_pqtids AS (
  93. SELECT pqtid, MIN(dt) AS first_conv_dt
  94. FROM loghubods.ad_own_open_conv
  95. WHERE dt BETWEEN '${window_start_dt}' AND '${window_end_dt}'
  96. AND pqtid IS NOT NULL AND pqtid <> ''
  97. GROUP BY pqtid
  98. ),
  99. ad_metrics AS (
  100. SELECT
  101. i.mid,
  102. COUNT(a.machinecode) AS own_ad_click_cnt,
  103. COUNT(DISTINCT CASE WHEN o.pqtid IS NOT NULL THEN a.pqtid END) AS long_press_scan_cnt
  104. FROM input_user i
  105. LEFT JOIN loghubods.ad_action_log_own a
  106. ON a.machinecode = i.mid
  107. AND a.dt BETWEEN '${window_start_dt}' AND '${window_end_dt}'
  108. AND a.businesstype = 'adClick'
  109. AND a.ownadsystemtype = 'ownPlatform'
  110. LEFT JOIN open_conv_pqtids o
  111. ON o.pqtid = a.pqtid
  112. AND o.first_conv_dt BETWEEN a.dt AND i.window_end_dt
  113. GROUP BY i.mid
  114. ),
  115. source_share_events AS (
  116. SELECT
  117. i.mid,
  118. s.shareid,
  119. MIN(CAST(s.clienttimestamp AS BIGINT)) AS share_ts
  120. FROM input_user i
  121. JOIN loghubods.user_share_log s
  122. ON s.machinecode = i.mid
  123. AND s.dt BETWEEN '${window_start_dt}' AND '${window_end_dt}'
  124. AND s.topic = 'share'
  125. AND s.shareid IS NOT NULL AND s.shareid <> ''
  126. AND s.clienttimestamp RLIKE '^[0-9]+$'
  127. GROUP BY i.mid, s.shareid
  128. ),
  129. return_click_events AS (
  130. SELECT
  131. c.shareid,
  132. c.machinecode AS return_mid,
  133. CAST(c.clienttimestamp AS BIGINT) AS click_ts
  134. FROM loghubods.user_share_log c
  135. JOIN (SELECT DISTINCT shareid FROM source_share_events) s
  136. ON c.shareid = s.shareid
  137. WHERE c.dt BETWEEN '${window_start_dt}' AND '${window_end_dt}'
  138. AND c.topic = 'click'
  139. AND c.machinecode IS NOT NULL AND c.machinecode <> ''
  140. AND c.clienttimestamp RLIKE '^[0-9]+$'
  141. ),
  142. return_metrics AS (
  143. SELECT
  144. i.mid,
  145. COUNT(DISTINCT s.shareid) AS window_shareid_cnt,
  146. COUNT(CASE WHEN c.click_ts >= s.share_ts AND c.return_mid <> s.mid
  147. THEN c.return_mid END) AS return_click_pv,
  148. COUNT(DISTINCT CASE WHEN c.click_ts >= s.share_ts AND c.return_mid <> s.mid
  149. THEN c.return_mid END) AS return_people_cnt
  150. FROM input_user i
  151. LEFT JOIN source_share_events s ON s.mid = i.mid
  152. LEFT JOIN return_click_events c ON c.shareid = s.shareid
  153. GROUP BY i.mid
  154. ),
  155. video_rows AS (
  156. SELECT
  157. i.mid,
  158. v.dt,
  159. v.machineinfo_system,
  160. v.machineinfo_brand,
  161. v.machineinfo_model,
  162. v.clientip,
  163. CAST(v.clienttimestamp AS BIGINT) AS event_ts
  164. FROM input_user i
  165. JOIN loghubods.video_action_log_applet v
  166. ON v.mid = i.mid
  167. AND v.dt BETWEEN '${window_start_dt}' AND '${window_end_dt}'
  168. AND v.business IN ('videoView','videoPlay','videoShareFriend','videoClick')
  169. ),
  170. attribute_metrics AS (
  171. SELECT
  172. mid,
  173. CONCAT_WS(',', COLLECT_SET(CASE WHEN machineinfo_system IS NOT NULL
  174. AND machineinfo_system <> '' THEN machineinfo_system END)) AS operating_system_list,
  175. CONCAT_WS(',', COLLECT_SET(CASE
  176. WHEN COALESCE(machineinfo_brand, '') <> '' OR COALESCE(machineinfo_model, '') <> ''
  177. THEN CONCAT_WS(' ', machineinfo_brand, machineinfo_model) END)) AS device_model_list,
  178. COUNT(DISTINCT CASE
  179. WHEN COALESCE(machineinfo_brand, '') <> '' OR COALESCE(machineinfo_model, '') <> ''
  180. THEN CONCAT_WS(' ', machineinfo_brand, machineinfo_model) END) AS device_model_cnt
  181. FROM video_rows
  182. GROUP BY mid
  183. ),
  184. daily_ip_ranked AS (
  185. SELECT
  186. mid,
  187. dt,
  188. clientip,
  189. ROW_NUMBER() OVER (PARTITION BY mid, dt ORDER BY event_ts DESC) AS rn
  190. FROM video_rows
  191. WHERE clientip IS NOT NULL AND clientip <> ''
  192. ),
  193. city_metrics AS (
  194. SELECT
  195. mid,
  196. CONCAT_WS(',', COLLECT_SET(CASE WHEN city IS NOT NULL AND city <> '' THEN city END))
  197. AS city_list,
  198. COUNT(DISTINCT CASE WHEN city IS NOT NULL AND city <> '' THEN city END) AS city_cnt
  199. FROM (
  200. SELECT mid, ANALYSISIP(clientip, 'city') AS city
  201. FROM daily_ip_ranked
  202. WHERE rn = 1
  203. ) t
  204. GROUP BY mid
  205. ),
  206. capture_adview_events AS (
  207. SELECT i.mid, a.dt AS event_dt, a.subsessionid,
  208. CAST(a.clienttimestamp AS BIGINT) AS adview_ts
  209. FROM input_user i JOIN loghubods.ad_action_log_own a
  210. ON a.machinecode = i.mid
  211. AND a.dt BETWEEN '${window_start_dt}' AND '${window_end_dt}'
  212. AND a.businesstype = 'adView'
  213. WHERE a.subsessionid IS NOT NULL AND a.subsessionid <> ''
  214. AND a.clienttimestamp RLIKE '^[0-9]+$'
  215. ),
  216. capture_events AS (
  217. SELECT i.mid, s.dt AS event_dt, s.subsessionid,
  218. CAST(s.clienttimestamp AS BIGINT) AS capture_ts
  219. FROM input_user i JOIN loghubods.simpleevent_log s
  220. ON s.machinecode = i.mid
  221. AND s.dt BETWEEN '${window_start_dt}' AND '${window_end_dt}'
  222. AND s.businesstype = 'userCaptureScreen'
  223. WHERE s.subsessionid IS NOT NULL AND s.subsessionid <> ''
  224. AND s.clienttimestamp RLIKE '^[0-9]+$'
  225. ),
  226. capture_close_events AS (
  227. SELECT i.mid, a.dt AS event_dt, a.subsessionid,
  228. CAST(a.clienttimestamp AS BIGINT) AS close_ts
  229. FROM input_user i JOIN loghubods.ad_action_log_own a
  230. ON a.machinecode = i.mid
  231. AND a.dt BETWEEN '${window_start_dt}' AND '${window_end_dt}'
  232. AND a.businesstype = 'adCloseBtnTap'
  233. ),
  234. capture_video_view_events AS (
  235. SELECT i.mid, v.dt AS event_dt, v.subsessionid,
  236. CAST(v.clienttimestamp AS BIGINT) AS event_ts
  237. FROM input_user i JOIN loghubods.video_action_log_applet v
  238. ON v.mid = i.mid
  239. AND v.dt BETWEEN '${window_start_dt}' AND '${window_end_dt}'
  240. AND v.business = 'videoView' AND v.businesstype = 'videoView'
  241. AND v.pagesource RLIKE 'category$'
  242. ),
  243. capture_video_click_events AS (
  244. SELECT i.mid, s.dt AS event_dt, s.subsessionid,
  245. CAST(s.clienttimestamp AS BIGINT) AS event_ts
  246. FROM input_user i JOIN loghubods.simpleevent_log s
  247. ON s.machinecode = i.mid
  248. AND s.dt BETWEEN '${window_start_dt}' AND '${window_end_dt}'
  249. AND s.businesstype = 'videoClick'
  250. ),
  251. capture_ad_click_events AS (
  252. SELECT i.mid, a.dt AS event_dt, a.subsessionid,
  253. CAST(a.clienttimestamp AS BIGINT) AS event_ts
  254. FROM input_user i JOIN loghubods.ad_action_log_own a
  255. ON a.machinecode = i.mid
  256. AND a.dt BETWEEN '${window_start_dt}' AND '${window_end_dt}'
  257. AND a.businesstype = 'adClick'
  258. ),
  259. matched_capture_events AS (
  260. SELECT DISTINCT a.mid, a.event_dt, a.subsessionid, c.capture_ts
  261. FROM capture_adview_events a
  262. JOIN capture_events c
  263. ON a.mid = c.mid AND a.event_dt = c.event_dt
  264. AND a.subsessionid = c.subsessionid AND c.capture_ts >= a.adview_ts
  265. WHERE NOT EXISTS (
  266. SELECT 1 FROM capture_close_events x
  267. WHERE x.mid = a.mid AND x.event_dt = a.event_dt
  268. AND x.subsessionid = a.subsessionid
  269. AND x.close_ts > a.adview_ts AND x.close_ts <= c.capture_ts
  270. )
  271. AND NOT EXISTS (
  272. SELECT 1 FROM capture_video_view_events x
  273. WHERE x.mid = a.mid AND x.event_dt = a.event_dt
  274. AND x.subsessionid = a.subsessionid
  275. AND x.event_ts > a.adview_ts AND x.event_ts <= c.capture_ts
  276. )
  277. AND NOT EXISTS (
  278. SELECT 1 FROM capture_video_click_events x
  279. WHERE x.mid = a.mid AND x.event_dt = a.event_dt
  280. AND x.subsessionid = a.subsessionid
  281. AND x.event_ts > a.adview_ts AND x.event_ts <= c.capture_ts
  282. )
  283. AND NOT EXISTS (
  284. SELECT 1 FROM capture_ad_click_events x
  285. WHERE x.mid = a.mid AND x.event_dt = a.event_dt
  286. AND x.subsessionid = a.subsessionid
  287. AND x.event_ts > a.adview_ts AND x.event_ts <= c.capture_ts
  288. )
  289. ),
  290. capture_metrics AS (
  291. SELECT mid, COUNT(*) AS ad_play_capture_cnt
  292. FROM matched_capture_events
  293. GROUP BY mid
  294. ),
  295. landing_capture_metrics AS (
  296. SELECT
  297. i.mid,
  298. COUNT(DISTINCT CASE WHEN a.machinecode IS NOT NULL THEN CONCAT(
  299. a.dt, '#', COALESCE(a.subsessionid, ''), '#', COALESCE(a.clienttimestamp, '')
  300. ) END) AS ad_landing_capture_cnt
  301. FROM input_user i
  302. LEFT JOIN loghubods.ad_action_log_own a
  303. ON a.machinecode = i.mid
  304. AND a.dt BETWEEN '${window_start_dt}' AND '${window_end_dt}'
  305. AND a.businesstype = 'adUserCaptureScreen'
  306. GROUP BY i.mid
  307. ),
  308. bg_adview_events AS (
  309. SELECT i.mid, a.dt AS event_dt, a.sessionid, a.subsessionid,
  310. CAST(a.clienttimestamp AS BIGINT) AS adview_ts
  311. FROM input_user i JOIN loghubods.ad_action_log_own a
  312. ON a.machinecode = i.mid
  313. AND a.dt BETWEEN '${window_start_dt}' AND '${window_end_dt}'
  314. AND a.businesstype = 'adView' AND a.ownadsystemtype = 'ownPlatform'
  315. WHERE a.sessionid IS NOT NULL AND a.sessionid <> ''
  316. AND a.subsessionid IS NOT NULL AND a.subsessionid <> ''
  317. AND a.clienttimestamp RLIKE '^[0-9]+$'
  318. ),
  319. active_end_events AS (
  320. SELECT i.mid, s.dt AS event_dt, s.sessionid, s.subsessionid,
  321. CAST(s.clienttimestamp AS BIGINT) AS active_end_ts
  322. FROM input_user i JOIN loghubods.simpleevent_log s
  323. ON s.machinecode = i.mid
  324. AND s.dt BETWEEN '${window_start_dt}' AND '${window_end_dt}'
  325. AND s.businesstype = 'userActiveEnd'
  326. AND s.endroutepath = 'pages/swiper/index'
  327. WHERE s.sessionid IS NOT NULL AND s.sessionid <> ''
  328. AND s.subsessionid IS NOT NULL AND s.subsessionid <> ''
  329. AND s.clienttimestamp RLIKE '^[0-9]+$'
  330. ),
  331. foreground_session_max AS (
  332. SELECT i.mid, u.dt AS event_dt, u.sessionid,
  333. MAX(CAST(u.clienttimestamp AS BIGINT)) AS last_foreground_ts
  334. FROM input_user i JOIN loghubods.useractive_log u
  335. ON u.machinecode = i.mid
  336. AND u.dt BETWEEN '${window_start_dt}' AND '${window_end_dt}'
  337. AND u.businesstype = 'path' AND u.path = 'pages/swiper/index'
  338. WHERE u.sessionid IS NOT NULL AND u.sessionid <> ''
  339. AND u.clienttimestamp RLIKE '^[0-9]+$'
  340. GROUP BY i.mid, u.dt, u.sessionid
  341. ),
  342. bg_close_events AS (
  343. SELECT i.mid, a.dt AS event_dt, a.subsessionid,
  344. CAST(a.clienttimestamp AS BIGINT) AS close_ts
  345. FROM input_user i JOIN loghubods.ad_action_log_own a
  346. ON a.machinecode = i.mid
  347. AND a.dt BETWEEN '${window_start_dt}' AND '${window_end_dt}'
  348. AND a.businesstype = 'adCloseBtnTap'
  349. ),
  350. home_pageview_events AS (
  351. SELECT i.mid, s.dt AS event_dt, s.subsessionid,
  352. CAST(s.clienttimestamp AS BIGINT) AS pageview_ts
  353. FROM input_user i JOIN loghubods.simpleevent_log s
  354. ON s.machinecode = i.mid
  355. AND s.dt BETWEEN '${window_start_dt}' AND '${window_end_dt}'
  356. AND s.businesstype = 'pageView' AND s.pagesource RLIKE 'category_55$'
  357. ),
  358. mini_program_open_events AS (
  359. SELECT i.mid, a.dt AS event_dt, a.subsessionid,
  360. CAST(a.clienttimestamp AS BIGINT) AS mini_open_ts
  361. FROM input_user i JOIN loghubods.ad_action_log_own a
  362. ON a.machinecode = i.mid
  363. AND a.dt BETWEEN '${window_start_dt}' AND '${window_end_dt}'
  364. AND a.businesstype = 'miniProgramOnOpen'
  365. ),
  366. bg_video_view_events AS (
  367. SELECT i.mid, v.dt AS event_dt, v.sessionid,
  368. CAST(v.clienttimestamp AS BIGINT) AS video_view_ts
  369. FROM input_user i JOIN loghubods.video_action_log_applet v
  370. ON v.mid = i.mid
  371. AND v.dt BETWEEN '${window_start_dt}' AND '${window_end_dt}'
  372. AND v.business = 'videoView' AND v.businesstype = 'videoView'
  373. ),
  374. matched_background_events AS (
  375. SELECT DISTINCT a.mid, a.event_dt, a.sessionid, a.subsessionid, e.active_end_ts
  376. FROM bg_adview_events a
  377. JOIN active_end_events e
  378. ON a.mid = e.mid AND a.event_dt = e.event_dt
  379. AND a.sessionid = e.sessionid AND a.subsessionid = e.subsessionid
  380. AND e.active_end_ts BETWEEN a.adview_ts AND a.adview_ts + 10000
  381. JOIN foreground_session_max f
  382. ON f.mid = a.mid AND f.event_dt = a.event_dt AND f.sessionid = a.sessionid
  383. AND f.last_foreground_ts > e.active_end_ts
  384. WHERE NOT EXISTS (
  385. SELECT 1 FROM bg_close_events x
  386. WHERE x.mid = a.mid AND x.event_dt = a.event_dt
  387. AND x.subsessionid = a.subsessionid
  388. AND x.close_ts > a.adview_ts AND x.close_ts <= e.active_end_ts
  389. )
  390. AND NOT EXISTS (
  391. SELECT 1 FROM home_pageview_events x
  392. WHERE x.mid = a.mid AND x.event_dt = a.event_dt
  393. AND x.subsessionid = a.subsessionid
  394. AND x.pageview_ts > a.adview_ts AND x.pageview_ts <= e.active_end_ts
  395. )
  396. AND NOT EXISTS (
  397. SELECT 1 FROM mini_program_open_events x
  398. WHERE x.mid = a.mid AND x.event_dt = a.event_dt
  399. AND x.subsessionid = a.subsessionid
  400. AND x.mini_open_ts > a.adview_ts AND x.mini_open_ts <= e.active_end_ts
  401. )
  402. AND NOT EXISTS (
  403. SELECT 1 FROM bg_video_view_events x
  404. WHERE x.mid = a.mid AND x.event_dt = a.event_dt
  405. AND x.sessionid = a.sessionid
  406. AND x.video_view_ts > e.active_end_ts
  407. AND x.video_view_ts <= f.last_foreground_ts
  408. )
  409. ),
  410. background_metrics AS (
  411. SELECT mid, COUNT(*) AS ad_play_background_return_cnt
  412. FROM matched_background_events
  413. GROUP BY mid
  414. ),
  415. landing_view AS (
  416. SELECT i.mid, a.dt AS event_dt, a.sessionid, a.subsessionid, a.pqtid,
  417. CAST(a.clienttimestamp AS BIGINT) AS view_ts
  418. FROM input_user i JOIN loghubods.ad_action_log_own a
  419. ON a.machinecode = i.mid
  420. AND a.dt BETWEEN '${window_start_dt}' AND '${window_end_dt}'
  421. AND a.businesstype = 'adSelfLandingView'
  422. AND a.ownadsystemtype = 'ownPlatform'
  423. WHERE a.sessionid IS NOT NULL AND a.sessionid <> ''
  424. AND a.subsessionid IS NOT NULL AND a.subsessionid <> ''
  425. AND a.pqtid IS NOT NULL AND a.pqtid <> ''
  426. AND a.clienttimestamp RLIKE '^[0-9]+$'
  427. ),
  428. landing_hide AS (
  429. SELECT i.mid, a.dt AS event_dt, a.sessionid, a.subsessionid, a.pqtid,
  430. CAST(a.clienttimestamp AS BIGINT) AS hide_ts
  431. FROM input_user i JOIN loghubods.ad_action_log_own a
  432. ON a.machinecode = i.mid
  433. AND a.dt BETWEEN '${window_start_dt}' AND '${window_end_dt}'
  434. AND a.businesstype = 'adSelfLandingHide'
  435. WHERE a.sessionid IS NOT NULL AND a.sessionid <> ''
  436. AND a.subsessionid IS NOT NULL AND a.subsessionid <> ''
  437. AND a.pqtid IS NOT NULL AND a.pqtid <> ''
  438. AND a.clienttimestamp RLIKE '^[0-9]+$'
  439. ),
  440. landing_open_conv AS (
  441. SELECT DISTINCT pqtid
  442. FROM loghubods.ad_own_open_conv
  443. WHERE dt BETWEEN '${window_start_dt}' AND '${window_end_dt}'
  444. AND pqtid IS NOT NULL AND pqtid <> ''
  445. ),
  446. return_landing_session_max AS (
  447. SELECT i.mid, u.dt AS event_dt, u.sessionid,
  448. MAX(CAST(u.clienttimestamp AS BIGINT)) AS last_return_ts
  449. FROM input_user i JOIN loghubods.useractive_log u
  450. ON u.machinecode = i.mid
  451. AND u.dt BETWEEN '${window_start_dt}' AND '${window_end_dt}'
  452. AND u.businesstype = 'path'
  453. AND u.path IN (
  454. 'pages/ad-self-landing/index','pages/marketing-landing/index',
  455. 'pages/promo-page/index','pages/event-landing/index'
  456. )
  457. WHERE u.sessionid IS NOT NULL AND u.sessionid <> ''
  458. AND u.clienttimestamp RLIKE '^[0-9]+$'
  459. GROUP BY i.mid, u.dt, u.sessionid
  460. ),
  461. matched_landing_events AS (
  462. SELECT DISTINCT v.mid, v.event_dt, v.sessionid, v.subsessionid, v.pqtid, h.hide_ts
  463. FROM landing_view v
  464. JOIN landing_hide h
  465. ON v.mid = h.mid AND v.event_dt = h.event_dt
  466. AND v.sessionid = h.sessionid AND v.subsessionid = h.subsessionid
  467. AND v.pqtid = h.pqtid
  468. AND h.hide_ts BETWEEN v.view_ts AND v.view_ts + 10000
  469. JOIN return_landing_session_max r
  470. ON r.mid = v.mid AND r.event_dt = v.event_dt AND r.sessionid = v.sessionid
  471. AND r.last_return_ts > h.hide_ts
  472. WHERE NOT EXISTS (
  473. SELECT 1 FROM landing_open_conv c WHERE c.pqtid = v.pqtid
  474. )
  475. ),
  476. landing_return_metrics AS (
  477. SELECT mid, COUNT(*) AS landing_no_scan_background_return_cnt
  478. FROM matched_landing_events
  479. GROUP BY mid
  480. )
  481. SELECT
  482. i.mid AS `用户id`,
  483. i.anchor_dt AS `锚点日期`,
  484. i.window_start_dt AS `统计开始日期`,
  485. i.window_end_dt AS `统计结束日期`,
  486. COALESCE(a.active_day_cnt, 0) AS `活跃天数`,
  487. COALESCE(c.card_click_object_uv, 0) AS `点击卡片去重次数`,
  488. COALESCE(c.card_click_object_pv, 0) AS `点击卡片不去重次数`,
  489. COALESCE(p.real_play_cnt, 0) AS `有效播放次数`,
  490. COALESCE(s.video_share_cnt, 0) AS `分享次数`,
  491. COALESCE(r.window_shareid_cnt, 0) AS `分享ID数`,
  492. COALESCE(r.return_click_pv, 0) AS `分享带回点击次数`,
  493. COALESCE(r.return_people_cnt, 0) AS `分享带回去重回流人数`,
  494. COALESCE(ad.own_ad_click_cnt, 0) AS `自营广告点击次数`,
  495. COALESCE(ad.long_press_scan_cnt, 0) AS `长按扫码转化次数`,
  496. COALESCE(cp.ad_play_capture_cnt, 0) AS `广告播放中截图次数`,
  497. COALESCE(lc.ad_landing_capture_cnt, 0) AS `广告落地页截图次数`,
  498. COALESCE(bg.ad_play_background_return_cnt, 0) AS `广告播放中切后台再返回次数`,
  499. COALESCE(lr.landing_no_scan_background_return_cnt, 0)
  500. AS `无扫码落地页切后台再返回次数`,
  501. COALESCE(c.source_1007_cnt, 0) AS `来源个人分享1007次数`,
  502. COALESCE(c.source_1008_cnt, 0) AS `来源群分享1008次数`,
  503. COALESCE(c.source_1058_cnt, 0) AS `来源公众号文章1058次数`,
  504. COALESCE(c.source_1074_cnt, 0) AS `来源公众号即转1074次数`,
  505. COALESCE(c.source_1067_1095_cnt, 0) AS `来源小程序投流1067_1095次数`,
  506. o.other_scene_list AS `其他来源场景值及次数`,
  507. c.source_shareid_list AS `来源上游shareid`,
  508. a.source_opengid_list AS `来源群id`,
  509. am.operating_system_list AS `操作系统`,
  510. am.device_model_list AS `设备机型`,
  511. COALESCE(am.device_model_cnt, 0) AS `设备机型数`,
  512. cm.city_list AS `历史地域`,
  513. COALESCE(cm.city_cnt, 0) AS `历史地域数`
  514. FROM input_user i
  515. LEFT JOIN active_metrics a ON a.mid = i.mid
  516. LEFT JOIN real_play_metrics p ON p.mid = i.mid
  517. LEFT JOIN share_metrics s ON s.mid = i.mid
  518. LEFT JOIN click_metrics c ON c.mid = i.mid
  519. LEFT JOIN other_scene_metrics o ON o.mid = i.mid
  520. LEFT JOIN ad_metrics ad ON ad.mid = i.mid
  521. LEFT JOIN return_metrics r ON r.mid = i.mid
  522. LEFT JOIN attribute_metrics am ON am.mid = i.mid
  523. LEFT JOIN city_metrics cm ON cm.mid = i.mid
  524. LEFT JOIN capture_metrics cp ON cp.mid = i.mid
  525. LEFT JOIN landing_capture_metrics lc ON lc.mid = i.mid
  526. LEFT JOIN background_metrics bg ON bg.mid = i.mid
  527. LEFT JOIN landing_return_metrics lr ON lr.mid = i.mid;