test_sql.sql 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. DROP TABLE IF EXISTS filter_rel_edge;
  2. DROP TABLE IF EXISTS temp_core_nodes;
  3. DROP TABLE IF EXISTS person_rel_edge_trans_filter;
  4. DROP TABLE IF EXISTS person_rel_edge_call_filter;
  5. DROP TABLE IF EXISTS person_rel_edge_other;
  6. DROP TABLE IF EXISTS person_rel_edge_call;
  7. DROP TABLE IF EXISTS person_rel_edge_trans;
  8. CREATE
  9. TEMP TABLE temp_core_nodes AS
  10. SELECT unnest(['513126197412174415','513126199410052413', '110101199003071233', '330382199003081853',
  11. '510183198103160051', '6222024505288690226', '1023254100000211',
  12. '6216603637176773705', '3521023012569874', '6223092984200995695',
  13. '6237683810003447983', '6237683810003442235', '6225881084437762',
  14. '6222027549499181296']) AS node_id,
  15. unnest(['obj','obj', 'obj', 'obj', 'obj', 'card', 'card', 'card', 'card', 'card', 'card', 'card', 'card',
  16. 'card']) AS node_type;
  17. CREATE
  18. TEMP TABLE filter_rel_edge AS
  19. SELECT *
  20. FROM person_rel_edge;
  21. CREATE
  22. TEMP TABLE person_rel_edge_trans_filter AS
  23. SELECT *
  24. FROM (SELECT *
  25. FROM filter_rel_edge
  26. WHERE clazz = 'card_card'
  27. AND fx = 1
  28. AND node1 IN (SELECT node_id FROM temp_core_nodes)
  29. AND node2 NOT IN (SELECT node_id FROM temp_core_nodes)
  30. UNION ALL
  31. SELECT *
  32. FROM filter_rel_edge
  33. WHERE clazz = 'card_card'
  34. AND fx = 2
  35. AND node1 NOT IN (SELECT node_id FROM temp_core_nodes)
  36. AND node2 IN (SELECT node_id FROM temp_core_nodes)) AS combined_edges
  37. where 1 = 1
  38. AND num::decimal >= 1
  39. AND num::decimal <= 2000;
  40. insert into temp_core_nodes
  41. select node_id, node_type
  42. from (select node1 as node_id, 'card' as node_type
  43. from person_rel_edge_trans_filter
  44. union all
  45. select node2 as node_id, 'card' as node_type
  46. from person_rel_edge_trans_filter) as temp_node_id
  47. group by node_id, node_type;
  48. CREATE
  49. TEMP TABLE person_rel_edge_call_filter AS
  50. SELECT *
  51. FROM (SELECT *
  52. FROM filter_rel_edge
  53. WHERE clazz = 'tel_tel'
  54. AND fx = 1
  55. AND node1 IN (SELECT node_id FROM temp_core_nodes)
  56. AND node2 NOT IN (SELECT node_id FROM temp_core_nodes)
  57. UNION ALL
  58. SELECT *
  59. FROM filter_rel_edge
  60. WHERE clazz = 'tel_tel'
  61. AND fx = 2
  62. AND node1 NOT IN (SELECT node_id FROM temp_core_nodes)
  63. AND node2 IN (SELECT node_id FROM temp_core_nodes)) AS combined_edges
  64. where num::integer >= 4;
  65. insert into temp_core_nodes
  66. select node_id, node_type
  67. from (select node1 as node_id, 'tel' as node_type
  68. from person_rel_edge_call_filter
  69. union all
  70. select node2 as node_id, 'tel' as node_type
  71. from person_rel_edge_call_filter) as temp_node_id
  72. group by node_id, node_type;
  73. SELECT node_id, node_type
  74. FROM temp_core_nodes
  75. GROUP BY ALL;
  76. SELECT max(id) as id,
  77. node1,
  78. node2,
  79. mc,
  80. clazz,
  81. SUM(COALESCE(num, 0)::DECIMAL) AS num,
  82. any_value(fx) as fx,
  83. any_value(lx) as lx
  84. FROM filter_rel_edge
  85. WHERE node1 IN (SELECT node_id FROM temp_core_nodes)
  86. AND node2 IN (SELECT node_id FROM temp_core_nodes)
  87. group by node1, node2, mc, clazz;
  88. SELECT id,
  89. node1,
  90. node2,
  91. mc,
  92. clazz,
  93. num,
  94. fx,
  95. lx
  96. FROM filter_rel_edge
  97. WHERE clazz not in ('card_card', 'tel_tel')
  98. AND node1 IN (SELECT node_id FROM temp_core_nodes)
  99. AND node2 IN (SELECT node_id FROM temp_core_nodes);
  100. --
  101. SELECT max(id) as id,
  102. node1,
  103. node2,
  104. mc,
  105. clazz,
  106. SUM(COALESCE(num, 0)::DECIMAL) AS num,
  107. any_value(fx) as fx,
  108. any_value(lx) as lx
  109. FROM filter_rel_edge
  110. WHERE clazz = 'card_card'
  111. AND node1 IN (SELECT node_id FROM temp_core_nodes)
  112. AND node2 IN (SELECT node_id FROM temp_core_nodes)
  113. group by node1, node2, mc, clazz;
  114. SELECT max(id) as id,
  115. node1,
  116. node2,
  117. mc,
  118. clazz,
  119. sum(num::integer) as num,
  120. any_value(fx) as fx,
  121. any_value(lx) as lx
  122. FROM filter_rel_edge
  123. WHERE clazz = 'tel_tel'
  124. AND node1 IN (SELECT node_id FROM temp_core_nodes)
  125. AND node2 IN (SELECT node_id FROM temp_core_nodes)
  126. group by node1, node2, mc, clazz;
  127. with filter_recipient as (select 0 as type,
  128. id,
  129. recipientName as personName,
  130. recipientCurrentAddress as location,
  131. sendDate
  132. from express_info
  133. WHERE recipientCurrentAddress = '滕王阁'),
  134. filter_sender as (select 1 as type, id, senderName as personName, senderCurrentAddress as location, sendDate
  135. from express_info
  136. WHERE senderCurrentAddress = '滕王阁'),
  137. union_data as (select * from filter_recipient union all select * from filter_sender)
  138. select *
  139. from union_data
  140. select personName,
  141. location,
  142. count(*) as expressCount,
  143. count() filter (type = 0) as recipientCount, count() filter (type = 1) as senderCount, string_agg(id, ',') as ids
  144. from union_data
  145. group by personName, location
  146. order by expressCount desc
  147. SELECT coalesce(personName, personPhone) as personName,
  148. personRegionId,
  149. personCellId,
  150. personStationName,
  151. (COUNT(*) FILTER (EXTRACT(HOUR FROM callTime::TIME) >= 6 AND EXTRACT(HOUR FROM callTime::TIME) < 11) + COUNT(*)
  152. FILTER (EXTRACT(HOUR FROM callTime::TIME) >=
  153. 20 OR
  154. EXTRACT(HOUR FROM callTime::TIME) <
  155. 20)) AS dayNightTotal
  156. FROM call_record
  157. WHERE personCellId IS NOT NULL
  158. AND personRegionId IS NOT NULL
  159. GROUP BY ALL
  160. having dayNightTotal > 1
  161. ORDER BY dayNightTotal DESC
  162. SELECT *
  163. FROM call_record
  164. WHERE personName = '白歌'
  165. AND callTime::TIME >= '22:00:00'
  166. AND callTime::TIME <= '23:00:00'
  167. AND otherName IS NULL;
  168. WITH monthly_stats AS (SELECT personName,
  169. personCardNo,
  170. COALESCE(otherName, '未知对手') AS otherName,
  171. otherCardNo,
  172. EXTRACT(YEAR FROM transDate) * 12 + EXTRACT(MONTH FROM transDate) AS ym,
  173. DATE_TRUNC('month', transDate) AS trans_month,
  174. COUNT(*) AS monthly_times,
  175. SUM(transAmount) AS monthly_amount
  176. FROM trans_record
  177. WHERE personName IN ('白歌')
  178. and otherName = '支付宝(中国)网络技术有限公司'
  179. GROUP BY ALL),
  180. continuous_group AS (SELECT personName,
  181. personCardNo,
  182. otherName,
  183. otherCardNo,
  184. monthly_times,
  185. trans_month,
  186. monthly_amount,
  187. ym - ROW_NUMBER()
  188. OVER ( PARTITION BY personName, personCardNo,otherName,otherCardNo ORDER BY trans_month ) AS cont_group_id
  189. FROM monthly_stats)
  190. SELECT personName,
  191. personCardNo,
  192. otherName,
  193. otherCardNo,
  194. cont_group_id as contGroupId,
  195. SUM(monthly_times) AS pairTotalTrans,
  196. SUM(monthly_amount) AS pairTotalAmount,
  197. COUNT(*) AS pairContMonthCount,
  198. MIN(trans_month) AS continuousStartMonth,
  199. MAX(trans_month) AS continuousEndMonth
  200. FROM continuous_group
  201. GROUP BY ALL
  202. HAVING COUNT(*) >= 6 LIMIT 200;
  203. CREATE SEQUENCE time_series_id_seq START 1;
  204. DROP TABLE IF EXISTS time_series_info;
  205. CREATE TABLE time_series_info AS
  206. select nextval('') as rowId,
  207. personName,
  208. otherName,
  209. seriesDateTime,
  210. tableNameEn,
  211. ext1,
  212. ext2,
  213. ext3,
  214. ext4,
  215. ext5,
  216. ext6,
  217. ext7,
  218. ext8,
  219. ext9
  220. from (SELECT personName AS personName,
  221. personStationLatitude AS ext9,
  222. personStationLongitude AS ext8,
  223. personStationName AS ext7,
  224. otherName AS otherName,
  225. personCellId AS ext6,
  226. id AS id,
  227. '通信记录' AS tableNameCn,
  228. callDateTime AS seriesDateTime,
  229. personRegionId AS ext5,
  230. callDirection AS ext4,
  231. 'call_record' AS tableNameEn,
  232. operatorId AS ext3,
  233. otherPhone AS ext2,
  234. personPhone AS ext1
  235. FROM call_record
  236. UNION ALL
  237. SELECT personName AS personName,
  238. null AS ext9,
  239. null AS ext8,
  240. null AS ext7,
  241. otherName AS otherName,
  242. transAmount AS ext6,
  243. id AS id,
  244. '交易记录' AS tableNameCn,
  245. transDateTime AS seriesDateTime,
  246. null AS ext5,
  247. debitCreditFlag AS ext4,
  248. 'trans_record' AS tableNameEn,
  249. bankName AS ext3,
  250. otherCardNo AS ext2,
  251. personCardNo AS ext1
  252. FROM trans_record
  253. UNION ALL
  254. SELECT personName AS personName,
  255. null AS ext9,
  256. null AS ext8,
  257. null AS ext7,
  258. null AS otherName,
  259. null AS ext6,
  260. id AS id,
  261. '住宿信息' AS tableNameCn,
  262. inDateTime AS seriesDateTime,
  263. addressDetail AS ext5,
  264. roomNo AS ext4,
  265. 'trans_record' AS tableNameEn,
  266. null AS ext3,
  267. hotelName AS ext2,
  268. personCertNo AS ext1
  269. FROM hotel_stay_info
  270. UNION ALL
  271. SELECT personName AS personName,
  272. null AS ext9,
  273. null AS ext8,
  274. null AS ext7,
  275. null AS otherName,
  276. null AS ext6,
  277. id AS id,
  278. '铁路行程' AS tableNameCn,
  279. depDateTime AS seriesDateTime,
  280. null AS ext5,
  281. destination AS ext4,
  282. 'train_ticket_info' AS tableNameEn,
  283. origin AS ext3,
  284. trainNo AS ext2,
  285. personCertNo AS ext1
  286. FROM train_ticket_info
  287. UNION ALL
  288. SELECT personName AS personName,
  289. null AS ext9,
  290. null AS ext8,
  291. null AS ext7,
  292. null AS otherName,
  293. null AS ext6,
  294. id AS id,
  295. '铁路行程' AS tableNameCn,
  296. depDateTime AS seriesDateTime,
  297. null AS ext5,
  298. destination AS ext4,
  299. 'together_train_info' AS tableNameEn,
  300. origin AS ext3,
  301. trainNo AS ext2,
  302. personCertNo AS ext1
  303. FROM together_train_info
  304. UNION ALL
  305. SELECT personName AS personName,
  306. null AS ext9,
  307. null AS ext8,
  308. null AS ext7,
  309. null AS otherName,
  310. null AS ext6,
  311. id AS id,
  312. '航班行程' AS tableNameCn,
  313. depDateTime AS seriesDateTime,
  314. null AS ext5,
  315. arrAirport AS ext4,
  316. 'flight_ticket_info' AS tableNameEn,
  317. depAirport AS ext3,
  318. ticketNo AS ext2,
  319. personCertNo AS ext1
  320. FROM flight_ticket_info
  321. UNION ALL
  322. SELECT personNameCn AS personName,
  323. null AS ext9,
  324. null AS ext8,
  325. null AS ext7,
  326. null AS otherName,
  327. null AS ext6,
  328. id AS id,
  329. '航班行程' AS tableNameCn,
  330. depDateTime AS seriesDateTime,
  331. null AS ext5,
  332. arrCode AS ext4,
  333. 'flight_departure_info' AS tableNameEn,
  334. depCode AS ext3,
  335. flightNo AS ext2,
  336. personCertNo AS ext1
  337. FROM flight_departure_info
  338. UNION ALL
  339. SELECT companionName AS personName,
  340. null AS ext9,
  341. null AS ext8,
  342. null AS ext7,
  343. null AS otherName,
  344. null AS ext6,
  345. id AS id,
  346. '航班行程' AS tableNameCn,
  347. flightDateTime AS seriesDateTime,
  348. null AS ext5,
  349. destination AS ext4,
  350. 'together_flight_info' AS tableNameEn,
  351. origin AS ext3,
  352. flightNo AS ext2,
  353. personCertNo AS ext1
  354. FROM together_flight_info
  355. UNION ALL
  356. SELECT personName AS personName,
  357. null AS ext9,
  358. null AS ext8,
  359. null AS ext7,
  360. null AS otherName,
  361. null AS ext6,
  362. id AS id,
  363. '汽车购票行程' AS tableNameCn,
  364. depDateTime AS seriesDateTime,
  365. null AS ext5,
  366. destination AS ext4,
  367. 'track_car_ticket_info' AS tableNameEn,
  368. origin AS ext3,
  369. bch AS ext2,
  370. personCertNo AS ext1
  371. FROM track_car_ticket_info
  372. UNION ALL
  373. SELECT recipientName AS personName,
  374. null AS ext9,
  375. null AS ext8,
  376. null AS ext7,
  377. senderName AS otherName,
  378. null AS ext6,
  379. id AS id,
  380. '快递收发' AS tableNameCn,
  381. sendDate AS seriesDateTime,
  382. null AS ext5,
  383. senderCurrentAddress AS ext4,
  384. 'express_info' AS tableNameEn,
  385. recipientCurrentAddress AS ext3,
  386. null AS ext2,
  387. null AS ext1
  388. FROM express_info) as t
  389. ORDER BY seriesDateTime;
  390. INSERT INTO rel_edge
  391. (node1,
  392. node2,
  393. node1cn,
  394. node2cn,
  395. mc,
  396. clazz,
  397. num,
  398. sj,
  399. fx,
  400. lx)
  401. SELECT CASE
  402. WHEN c.callDirection = '主叫' THEN c.personPhone
  403. ELSE c.otherPhone
  404. END AS node1,
  405. CASE
  406. WHEN c.callDirection = '主叫' THEN c.otherPhone
  407. ELSE c.personPhone
  408. END AS node2,
  409. CASE
  410. WHEN c.callDirection = '主叫' THEN c.personName
  411. ELSE c.otherName
  412. END AS node1cn,
  413. CASE
  414. WHEN c.callDirection = '主叫' THEN c.otherName
  415. ELSE c.personName
  416. END AS node2cn,
  417. CASE
  418. WHEN c.callDirection = '主叫' THEN 1
  419. ELSE 2
  420. END AS fx,
  421. '通信' AS mc,
  422. 'tel_tel' AS clazz,
  423. '1' AS num,
  424. c.callDateTime AS sj,
  425. 1 AS lx
  426. FROM call_record as c limit 500;