rankingCalculator.dos 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  1. module fundit::rankingCalculator
  2. use fundit::sqlUtilities
  3. use fundit::operationDataPuller;
  4. use fundit::performanceDataPuller;
  5. use fundit::dataSaver
  6. /*
  7. * 汇集所有参与排名的指标信息
  8. *
  9. */
  10. def get_indicator_info() {
  11. ids = [1,
  12. 2, 6, 9, 10, 11, 12, 21, 50, 52, 59,
  13. 14, 15, 16, 17, 18, 19, 40, 58,
  14. 37, 38, 41, 42, 43, 44, 45, 46, 47, 48, 49,
  15. 33, 34, 35, 36,
  16. 66, 53, 54, 55, 56, 57,
  17. 71, 72, 73
  18. ];
  19. names = ['ret',
  20. 'maxdrawdown', 'kurtosis', 'skewness', 'stddev', 'alpha', 'beta', 'downsidedev', 'maxdrawdown_months', 'maxdrawdown_recoverymonths', 'winrate',
  21. 'kapparatio', 'treynorratio', 'jensen', 'omegaratio', 'sharperatio', 'sortinoratio_MAR', 'calmarratio', 'sortinoratio',
  22. 'per_con', 'info_ratio', 'var', 'cvar', 'smddvar', 'smddcvar', 'smdd_lpm1', 'smdd_lpm2', 'smdd_downside_dev', 'tracking_error', 'm2',
  23. 'upsidecapture_ret', 'downsidecapture_ret', 'upsidecapture_ratio', 'downsidecapture_ratio',
  24. 'stability', 'jc_stddev', 'gzstyle_stddev', 'gzstrategy_stddev', 'zz_stddev', 'zx_stddev',
  25. 'ms_return', 'ms_rar', 'ms_risk'
  26. ];
  27. is_ASCs = [false,
  28. true, true, false, true, false, false, true, true, true, false,
  29. false, false, false, false, false, false, false, false,
  30. false, false, true, true, true, true, true, true, true, true, false,
  31. false, false, false, true,
  32. true, true, true, true, true, true,
  33. false, false, true
  34. ];
  35. return table(names AS name, ids AS id, is_ASCs AS is_ASC);
  36. }
  37. /*
  38. * 自定义百分位计算
  39. *
  40. */
  41. defg perRank(x, is_ASC) {
  42. return (100 * x.rank(ascending=is_ASC, percent=true)).round(0);
  43. }
  44. /*
  45. * 动态生成用于排序的SQL脚本
  46. *
  47. * @param data_table <TABLE>: 指标横表
  48. * @param indicator_table <TABLE>: 指标表,有 id, name, is_ASC 字段
  49. *
  50. * TODO: portfolio, cf, manager, company,
  51. * TODO: bfi & category
  52. *
  53. */
  54. def gen_ranking_sql(entity_type, data_table, indicator_table) {
  55. ranking = iif(entity_type IN ['PL', 'CO'], create_mc_indicator_ranking(), create_entity_indicator_ranking());
  56. ranking_num = iif(entity_type IN ['PL', 'CO'], create_mc_indicator_ranking_num(), create_entity_indicator_ranking_num());
  57. if(entity_type IN ['PL', 'CO'])
  58. v_groupby = ['curve_type', 'strategy', 'category_id', 'end_date'];
  59. else
  60. v_groupby = ['category_id', 'end_date'];
  61. for(indicator in indicator_table) {
  62. // 与 MySQL 不同,这里统一把近4年和成立以来的排名去掉
  63. if(indicator.id == 1) {
  64. v_trailing = ['1m', '3m', '6m', '1y', '2y', '3y', '5y', '10y', 'ytd'];
  65. // 晨星指标只有3,5,10年
  66. } else if(indicator.id in (71, 72, 73)) {
  67. v_trailing = ['3y', '5y', '10y'];
  68. v_missing_trailing = ['1m', '3m', '6m', '1y', '2y', 'ytd'];
  69. } else {
  70. v_trailing = ['6m', '1y', '2y', '3y', '5y', '10y', 'ytd'];
  71. v_missing_trailing = ['1m', '3m'];
  72. }
  73. // 绝对排名和百分位排名
  74. t_ranking = sql(select = (sqlCol(['entity_id'].join(v_groupby)), <indicator.id as indicator_id>,
  75. sqlCol(indicator.name + '_' + v_trailing,, 'indicator_' + v_trailing),
  76. sqlCol(indicator.name + '_' + v_trailing, rank{, indicator.is_ASC}, 'absrank_' + v_trailing),
  77. sqlCol(indicator.name + '_' + v_trailing, perRank{, indicator.is_ASC}, 'perrank_' + v_trailing)
  78. ),
  79. from = data_table,
  80. where = < category_id IS NOT NULL>,
  81. groupBy = sqlCol(v_groupby),
  82. groupFlag = 0 ).eval(); // context by
  83. // 为了满足表结构的要求, 非收益的指标要补上1m和3m的字段,虽然都是NULL
  84. if(indicator.id != 1) {
  85. v_tmp_col = ['indicator_' + v_missing_trailing, 'absrank_' + v_missing_trailing, 'perrank_' + v_missing_trailing].flatten();
  86. v_tmp_type = [take(DOUBLE, v_missing_trailing.size()), take(INT, v_missing_trailing.size()), take(INT, v_missing_trailing.size())].flatten();
  87. t_ranking.addColumn(v_tmp_col, v_tmp_type);
  88. }
  89. t_ranking.reorderColumns!(ranking.colNames());
  90. ranking.tableInsert(t_ranking);
  91. // 平均值、集合数量、各分位的阈值
  92. t_ranking_num = sql(select =(sqlCol(v_groupby),
  93. iif(entity_type IN ['PL', 'CO'], <indicator.id as indicator_id>, // 取消基金经理/公司的 raise_type 字段
  94. [sqlCol('raise_type', mean, 'raise_type'), <indicator.id as indicator_id>]),
  95. sqlCol(indicator.name + '_' + v_trailing, mean, 'avg_' + v_trailing),
  96. sqlCol(indicator.name + '_' + v_trailing, count, 'avg_' + v_trailing + '_cnt'),
  97. sqlCol(indicator.name + '_' + v_trailing, percentile{, iif(indicator.is_ASC, 5, 95)}, 'perrank_percent_5_' + v_trailing),
  98. sqlCol(indicator.name + '_' + v_trailing, percentile{, iif(indicator.is_ASC, 10, 90)}, 'perrank_percent_10_' + v_trailing),
  99. sqlCol(indicator.name + '_' + v_trailing, percentile{, iif(indicator.is_ASC, 25, 75)}, 'perrank_percent_25_' + v_trailing),
  100. sqlCol(indicator.name + '_' + v_trailing, percentile{, iif(indicator.is_ASC, 50, 50)}, 'perrank_percent_50_' + v_trailing),
  101. sqlCol(indicator.name + '_' + v_trailing, percentile{, iif(indicator.is_ASC, 75, 25)}, 'perrank_percent_75_' + v_trailing),
  102. sqlCol(indicator.name + '_' + v_trailing, percentile{, iif(indicator.is_ASC, 90, 10)}, 'perrank_percent_90_' + v_trailing),
  103. sqlCol(indicator.name + '_' + v_trailing, percentile{, iif(indicator.is_ASC, 95, 5)}, 'perrank_percent_95_' + v_trailing),
  104. sqlCol(indicator.name + '_' + v_trailing, iif(indicator.is_ASC, min, max), 'best_' + v_trailing),
  105. sqlCol(indicator.name + '_' + v_trailing, iif(indicator.is_ASC, max, min), 'worst_' + v_trailing)
  106. ),
  107. from = data_table,
  108. where = < category_id IS NOT NULL>,
  109. groupBy = sqlCol(v_groupby),
  110. groupFlag = 1).eval(); // group by
  111. // 为了满足表结构的要求, 非收益的指标要补上1m和3m的字段,虽然都是NULL
  112. if(indicator.id != 1) {
  113. v_tmp_col = ['avg_' + v_missing_trailing, 'avg_' + v_missing_trailing + '_cnt', 'perrank_percent_5_' + v_missing_trailing,
  114. 'perrank_percent_10_' + v_missing_trailing, 'perrank_percent_25_' + v_missing_trailing,
  115. 'perrank_percent_50_' + v_missing_trailing, 'perrank_percent_75_' + v_missing_trailing,
  116. 'perrank_percent_90_' + v_missing_trailing, 'perrank_percent_95_' + v_missing_trailing,
  117. 'best_' + v_missing_trailing, 'worst_' + v_missing_trailing
  118. ].flatten();
  119. v_tmp_type = [take(DOUBLE, v_missing_trailing.size()), take(INT, v_missing_trailing.size()), take(DOUBLE, v_missing_trailing.size()),
  120. take(DOUBLE, v_missing_trailing.size()), take(DOUBLE, v_missing_trailing.size()),
  121. take(DOUBLE, v_missing_trailing.size()), take(DOUBLE, v_missing_trailing.size()),
  122. take(DOUBLE, v_missing_trailing.size()), take(DOUBLE, v_missing_trailing.size()),
  123. take(DOUBLE, v_missing_trailing.size()),take(DOUBLE, v_missing_trailing.size())
  124. ].flatten();
  125. t_ranking_num.addColumn(v_tmp_col, v_tmp_type);
  126. }
  127. t_ranking_num.reorderColumns!(ranking_num.colNames());
  128. ranking_num.tableInsert(t_ranking_num);
  129. }
  130. return ranking, ranking_num;
  131. }
  132. /*
  133. * 运行排名SQL脚本
  134. *
  135. *
  136. */
  137. def run_ranking_sql(entity_type, ranking_by, mutable data_table, indicator_table) {
  138. // data_table = t
  139. // ranking_by = 'strategy'
  140. ret = array(ANY, 0);
  141. if(ranking_by == 'bfi') {
  142. UPDATE data_table SET category_id = factor_id;
  143. v_ranking = gen_ranking_sql(entity_type, data_table, indicator_table);
  144. ret.append!(v_ranking[0]); // ranking table
  145. ret.append!(v_ranking[1]); // ranking_num table
  146. } else {
  147. // 策略排名
  148. UPDATE data_table SET category_id = strategy$STRING;
  149. v_ranking = gen_ranking_sql(entity_type, data_table, indicator_table);
  150. ret.append!(v_ranking[0]); // ranking table
  151. ret.append!(v_ranking[1]); // ranking_num table
  152. // 基金经理和公司没有子策略排名
  153. if(! (entity_type IN ['PL', 'CO']) ) {
  154. // 子策略排名
  155. UPDATE data_table SET category_id = substrategy$STRING;
  156. v_ranking = gen_ranking_sql(entity_type, data_table, indicator_table);
  157. ret.append!(v_ranking[0]); // ranking table
  158. ret.append!(v_ranking[1]); // ranking_num table
  159. }
  160. }
  161. return ret;
  162. }
  163. /*
  164. * 为排名做数据准备
  165. *
  166. * TODO: 对少量组合做优化
  167. *
  168. * @return <VECTOR>: 包含两个表,一个指标数据表,一个是指标信息表
  169. *
  170. */
  171. def prepare_data_for_ranking(ranking_by, entity_type, entity_info, end_date, isFromMySQL=true) {
  172. // return
  173. table_desc = get_performance_table_description(entity_type);
  174. tb_data_return = get_monthly_indicator_data(table_desc.table_name[0], end_date, isFromMySQL);
  175. entity_id_name = table_desc.sec_id_col[0];
  176. // risk
  177. table_desc = get_risk_stats_table_description(entity_type);
  178. tb_data_risk_stats = get_monthly_indicator_data(table_desc.table_name[0], end_date, isFromMySQL);
  179. // risk adjusted return
  180. table_desc = get_riskadjret_stats_table_description(entity_type);
  181. tb_data_riskadjret_stats = get_monthly_indicator_data(table_desc.table_name[0], end_date, isFromMySQL);
  182. // others
  183. table_desc = get_indicator_table_description(entity_type);
  184. tb_data_indicator_stats = get_monthly_indicator_data(table_desc.table_name[0], end_date, isFromMySQL);
  185. // 做个大宽表
  186. if(entity_type IN ['PL', 'CO']) {
  187. matchingCols = [entity_id_name, 'curve_type', 'strategy', 'end_date'];
  188. matchingCols2 = [entity_id_name, 'curve_type', 'strategy', 'end_date', 'factor_id'];
  189. } else {
  190. matchingCols = [entity_id_name, 'end_date'];
  191. matchingCols2 = [entity_id_name, 'end_date', 'factor_id'];
  192. }
  193. tb_data = lj(lj(lj(tb_data_return, tb_data_indicator_stats, matchingCols), tb_data_risk_stats, matchingCols), tb_data_riskadjret_stats, matchingCols);
  194. if(ranking_by == 'bfi') {
  195. // 去掉被移到 fund_ty_bfi_bm_indicator 表中的重复字段
  196. v_dups = [38, 48, 11, 12, 59, 16];
  197. v_dup_col = EXEC name + suffix
  198. FROM cj(get_indicator_info(), table(['_6m', '_1y', '_2y', '_3y', '_5y', '_10y', '_ytd'] AS suffix))
  199. WHERE id IN v_dups;
  200. tb_data.dropColumns!(v_dup_col);
  201. // bfi table
  202. table_desc = get_bfi_by_category_group_table_description(entity_type);
  203. tb_bfi = get_monthly_indicator_data(table_desc.table_name[0], end_date, isFromMySQL);
  204. // bfi (as benchmark) indicator
  205. table_desc = get_bfi_indicator_table_description(entity_type);
  206. tb_data_bfi_indicator = get_monthly_indicator_data(table_desc.table_name[0], end_date, isFromMySQL);
  207. tb_data = lj(ej(tb_data, tb_bfi, matchingCols), tb_data_bfi_indicator, matchingCols2);
  208. v_indicator_id = [1, // 对应 fund_performance, 取消39(年化收益) 因为没有意义
  209. 41, 42, 49, // 对应 fund_indicator, 取消37 (per_con), 43, 44, 45, 46, 47 (smdd模型) 因为dolphin 未计算
  210. 2, 6, 9, 10, 21, // 对应 fund_risk_stats, 取消50, 52 因为 dolphin 未计算
  211. 14, 15, 17, 18, 40, 58, // 对应 fund_riskadjret_stats 取消19 (MAR Sortino ratio) 因为 dolphin 未计算
  212. 11, 12, 16, 33, 34, 35, 36, 38, 48, 59 // 对应 fund_ty_bfi_bm_indicator
  213. ]; // 取消 pf_fund_factor_stability 66 (stabiliy) 因为 dolphin 未计算
  214. // 取消 fund_rbsa_style 53, 54, 55, 56, 57(风格稳定性) 因为 dolphin 未计算
  215. } else {
  216. // upside/downside capture
  217. table_desc = get_capture_style_table_description(entity_type);
  218. tb_data_capture_stats = get_monthly_indicator_data(table_desc.table_name[0], end_date, isFromMySQL);
  219. tb_data = lj(tb_data, tb_data_capture_stats, matchingCols);
  220. // morningstar data
  221. table_desc = get_ms_stats_table_description(entity_type);
  222. tb_data_ms_stats = get_monthly_indicator_data(table_desc.table_name[0], end_date, isFromMySQL);
  223. tb_data = lj(tb_data, tb_data_ms_stats, matchingCols);
  224. v_indicator_id = [1, // 对应 fund_performance, 取消39(年化收益) 因为没有意义
  225. 38, 41, 42, 48, 49, // 对应 fund_indicator, 取消37 (per_con), 43, 44, 45, 46, 47 (smdd模型) 因为dolphin 未计算
  226. 2, 6, 9, 10, 11, 12, 21, 59, // 对应 fund_risk_stats, 取消50, 52 因为 dolphin 未计算
  227. 14, 15, 16, 17, 18, 40, 58, // 对应 fund_riskadjret_stats 取消19 (MAR Sortino ratio) 因为 dolphin 未计算
  228. 33, 34, 35, 36, // 对应 fund_style_stats
  229. 71, 72, 73 // 对应 fund_ms_stats
  230. ];
  231. }
  232. tb_data.rename!(entity_id_name, 'entity_id');
  233. if(entity_type IN ['PL', 'CO'])
  234. t = SELECT * FROM entity_info en
  235. INNER JOIN tb_data d ON en.entity_id = d.entity_id AND en.curve_type = d.curve_type AND en.strategy = d.strategy
  236. WHERE en.strategy IS NOT NULL;
  237. else
  238. t = SELECT * FROM entity_info en
  239. INNER JOIN tb_data d ON en.entity_id = d.entity_id
  240. WHERE en.strategy IS NOT NULL;
  241. if(ranking_by == 'bfi')
  242. UPDATE t SET category_id = factor_id;
  243. else if(ranking_by == 'substrategy')
  244. UPDATE t SET category_id = substrategy$STRING;
  245. else
  246. UPDATE t SET category_id = strategy$STRING;
  247. indicator_table = SELECT * FROM get_indicator_info() WHERE id IN v_indicator_id;
  248. return t, indicator_table;
  249. }
  250. /*
  251. * 通用指标排名计算
  252. *
  253. * @param ranking_by <STRING>: strategy, bfi
  254. *
  255. */
  256. def cal_indicator_ranking(ranking_by, entity_type, entity_info, end_date, isFromMySQL=true) {
  257. // 当前只对基金做排名, 其它类型参考基金排名做相对排名
  258. if(!(entity_type in ['MF', 'HF', 'PL', 'CO'])) return null;
  259. v = prepare_data_for_ranking(ranking_by, entity_type, entity_info, end_date, isFromMySQL);
  260. v_ranking_tables = run_ranking_sql(entity_type, ranking_by, v[0], v[1]);
  261. return v_ranking_tables;
  262. }
  263. /*
  264. * 将源指标表横表变竖表,以方便参考排名计算
  265. *
  266. *
  267. */
  268. def run_transformation_sql(entity_type, data_table, ranking_by, indicator_info) {
  269. // 只有 portfolio_id 是整型,其它的都是字符串
  270. is_id_integer = false;
  271. if(entity_type == 'PF') is_id_integer = true;
  272. tb_ranking = create_entity_indicator_ranking(is_id_integer);
  273. for(indicator in indicator_info) {
  274. // 只有收益需要1m, 3m
  275. if(indicator.id == 1)
  276. v_trailing = ['1m', '3m', '6m', '1y', '2y', '3y', '5y', '10y', 'ytd'];
  277. // 晨星指标只有3,5,10年
  278. else if(indicator.id in (71, 72, 73)) {
  279. v_trailing = ['3y', '5y', '10y'];
  280. v_missing = ['1m', '3m', '6m', '1y', '2y', 'ytd'];
  281. }
  282. else {
  283. v_trailing = ['6m', '1y', '2y', '3y', '5y', '10y', 'ytd'];
  284. v_missing = ['1m', '3m'];
  285. }
  286. t = sql(select = (sqlCol(['entity_id', 'end_date', 'category_id']), <indicator.id as indicator_id>,
  287. sqlCol(indicator.name + '_' + v_trailing,, 'indicator_' + v_trailing)
  288. ),
  289. from = data_table
  290. ).eval();
  291. // 给非收益指标补上缺失的指标
  292. if(indicator.id != 1 )
  293. {
  294. v_tmp_col = ['indicator_' + v_missing, 'absrank_' + v_missing, 'perrank_' + v_missing].flatten();
  295. v_tmp_type = [take(DOUBLE, v_missing.size()), take(INT, v_missing.size()), take(INT, v_missing.size())].flatten();
  296. t.addColumn(v_tmp_col, v_tmp_type);
  297. }
  298. // 给所有指标补上 absrank 和 perrank 两套指标
  299. v_tmp_col = ['absrank_' + v_trailing, 'perrank_' + v_trailing].flatten();
  300. v_tmp_type = [take(INT, v_trailing.size()), take(INT, v_trailing.size())].flatten();
  301. t.addColumn(v_tmp_col, v_tmp_type);
  302. INSERT INTO tb_ranking
  303. SELECT * FROM (sql(select = sqlCol(tb_ranking.colNames()),
  304. from = t).eval());
  305. }
  306. return tb_ranking;
  307. }
  308. /*
  309. * 将源风险指标表横表变竖表,以方便排名计算
  310. *
  311. *
  312. */
  313. def transform_data_for_ranking (entity_type, entity_info, end_date, ranking_by, isFromMySQL=true) {
  314. if(entity_info.isVoid() || entity_info.size() == 0) return null;
  315. v = prepare_data_for_ranking(ranking_by, entity_type, entity_info, end_date, isFromMySQL);
  316. tb_ranking = run_transformation_sql(entity_type, v[0], ranking_by, v[1]);
  317. return tb_ranking;
  318. }
  319. /*
  320. *
  321. * 参考某指定类排名,计算相对排名
  322. *
  323. * @param benchmark_ranking <TABLE>: 被参考的排名表,如公募混合基金
  324. * @param entity_ranking <TABLE>: 被计算的指标表,排名被填充在原表中
  325. * @param isFromMySQL <BOOL>
  326. *
  327. *
  328. * Example: cal_relative_ranking(get_fund_indicator_ranking(NULL, 2024.09M, 102, true),
  329. * transform_risk_stats_for_ranking('PF', get_entity_info('PF', NULL), 2024.09M, true),
  330. * true);
  331. */
  332. def cal_relative_ranking(benchmark_ranking, mutable entity_ranking, isFromMySQL=true) {
  333. v_trailing = ['1m', '3m', '6m', '1y', '2y', '3y', '5y', '10y', 'ytd'];
  334. for(tr in v_trailing) {
  335. indicator_val_col = 'indicator_' + tr;
  336. // 乘上100,000 是为了满足 window join 的字段必须是INT或DURATION
  337. tb_tmp = sql(select = (sqlCol(['entity_id', 'end_date', 'category_id', 'indicator_id']),
  338. sqlColAlias(makeCall(round, binaryExpr(sqlCol(indicator_val_col), 1000000, *), 0), indicator_val_col + '_int')),
  339. from = entity_ranking,
  340. where = < _$indicator_val_col is not null >,
  341. orderBy = sqlCol(['end_date', 'category_id', 'indicator_id', indicator_val_col])
  342. ).eval();
  343. tb_tmp2 = sql(select = (sqlCol(['end_date', 'category_id', 'indicator_id']),
  344. sqlColAlias(makeCall(round, binaryExpr(sqlCol(indicator_val_col), 1000000, *), 0), indicator_val_col + '_int'),
  345. sqlCol('absrank_' + tr), sqlCol('perrank_' + tr)
  346. ),
  347. from = benchmark_ranking,
  348. where = < _$indicator_val_col is not null >,
  349. orderBy = sqlCol(['end_date', 'category_id', 'indicator_id', indicator_val_col])
  350. ).eval();
  351. absrank_col = 'absrank_' + tr;
  352. perrank_col = 'perrank_' + tr;
  353. // 用 pwj 来找最接近的排名
  354. tb_tmp_ranking = sql(select = (sqlCol(['entity_id', 'end_date', 'category_id', 'indicator_id']),
  355. sqlCol(indicator_val_col + '_int'),
  356. sqlCol(['absrank_max', 'perrank_max'])),
  357. from = pwj(tb_tmp, tb_tmp2,
  358. window = 0:1,
  359. aggs = [<max(_$absrank_col) as 'absrank_max'>, <max(_$perrank_col) as 'perrank_max'>],
  360. matchingCols = ['end_date', 'category_id', 'indicator_id', indicator_val_col + '_int'])
  361. ).eval();
  362. // 计算的结果填入排名表
  363. sqlUpdate(table = entity_ranking,
  364. updates = [<absrank_max as _$absrank_col>, <perrank_max as _$perrank_col>],
  365. from = <ej(entity_ranking, tb_tmp_ranking, ['entity_id', 'end_date', 'category_id','indicator_id'])>
  366. ).eval();
  367. }
  368. }
  369. /*
  370. * 排名数据入库
  371. *
  372. * @param ranking_by <STRING>: 'strategy', 'bfi'
  373. * @param ranking_tables <VECTOR>: 当 ranking_by = 'strategy' 时包含4个数据表的向量,分别是一级策略排名,一级策略排名阈值,二级策略排名,二级策略排名阈值
  374. * ranking_by = 'bfi' 时包含2个数据表的向量,分别是bfi策略排名,bfi策略排名阈值
  375. */
  376. def save_ranking_tables(entity_type, ranking_by, ranking_tables) {
  377. if(ranking_tables.isVoid()) return;
  378. des_strategy = get_indicator_ranking_table_description(entity_type)[0];
  379. des_bfi = get_bfi_bm_indicator_ranking_table_description(entity_type)[0];
  380. entity_id_col = des_strategy.sec_id_col;
  381. t = ranking_tables[0];
  382. if(ranking_by == 'bfi') {
  383. source_table = des_bfi.table_name.strReplace('pfdb', 'raw_db');
  384. target_table = des_bfi.table_name.strReplace('pfdb', 'raw_db');
  385. category_id_col = 'factor_id';
  386. t.rename!(['entity_id', 'category_id'], [entity_id_col, category_id_col]);
  387. } else {
  388. source_table = des_strategy.table_name.strReplace('pfdb', 'raw_db');
  389. target_table = des_strategy.table_name.strReplace('pfdb', 'raw_db');
  390. category_id_col = 'strategy';
  391. // 基金经理和公司自带 strategy 字段,而 category_id 就是 strategy 的复制,所以要去掉
  392. // 基金和组合则没有这个字段,将 category_id 改名改回 strategy
  393. if(entity_type IN ['PL', 'CO'])
  394. t.rename!('entity_id', entity_id_col).dropColumns!('category_id');
  395. else
  396. t.rename!(['entity_id', 'category_id'], [entity_id_col, category_id_col]);
  397. }
  398. save_and_sync(t, source_table, target_table);
  399. t = ranking_tables[1];
  400. if(ranking_by == 'bfi') {
  401. t.rename!('category_id', category_id_col);
  402. } else {
  403. if(entity_type IN ['PL', 'CO'])
  404. t.dropColumns!('category_id');
  405. else
  406. t.rename!('category_id', category_id_col);
  407. }
  408. save_and_sync(t, source_table + '_num', target_table + '_num');
  409. // 基金有二级策略排名
  410. if(ranking_by == 'strategy' && entity_type IN ['HF', 'MF']) {
  411. source_table = source_table.strReplace('_ranking', '_substrategy_ranking');
  412. target_table = target_table.strReplace('_ranking', '_substrategy_ranking');
  413. category_id_col = 'substrategy';
  414. t = ranking_tables[2];
  415. save_and_sync(t.rename!(['entity_id', 'category_id'], [entity_id_col, category_id_col]), source_table, target_table);
  416. t = ranking_tables[3];
  417. save_and_sync(t.rename!('category_id', category_id_col), source_table + '_num', target_table + '_num');
  418. }
  419. }
  420. /*
  421. * 参考排名数据入库
  422. *
  423. * @param ranking_tables <TABLE>:
  424. */
  425. def save_relative_ranking_table(entity_type, ranking_table, ranking_by) {
  426. if(ranking_table.isVoid()) return;
  427. source_table = '';
  428. target_table = '';
  429. if(entity_type == 'PF') {
  430. entity_id_col = 'portfolio_id';
  431. if(ranking_by == 'strategy') {
  432. source_table = 'raw_db.pf_portfolio_indicator_ranking';
  433. target_table = 'raw_db.pf_portfolio_indicator_ranking';
  434. } else if(ranking_by == 'substrategy') {save_relative_ranking_table
  435. source_table = 'raw_db.pf_portfolio_indicator_substrategy_ranking';
  436. target_table = 'raw_db.pf_portfolio_indicator_substrategy_ranking';
  437. } else if(ranking_by == 'bfi') {
  438. source_table = 'raw_db.pf_portfolio_bfi_bm_indicator_ranking';
  439. target_table = 'raw_db.pf_portfolio_bfi_bm_indicator_ranking';
  440. }
  441. } else if(entity_type == 'CF') {
  442. entity_id_col = 'fund_id';
  443. source_table = 'raw_db.pf_cus_fund_indicator_ranking';
  444. target_table = 'raw_db.pf_cus_fund_indicator_ranking'
  445. }
  446. save_and_sync(ranking_table, source_table, target_table);
  447. }