rankingCalculator.dos 21 KB

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