其他分享
首页 > 其他分享> > 代码优化ing

代码优化ing

作者:互联网

如果需要根据同表同字段中的不同条件去查找数据,可以使用pivot函数进行优化,如我需要查前10的数据,subValueFee,subValueRate为DM_IGW_MON_INDEX_VALUE_INFO 中SUB_NO字段为条件查询对应的SUB_VALUE值,第一种方法用的是左连接,sql较为长,且有许多重复的sql条件
在这里插入图片描述
select a.proSimple “proSimple” , round(a.sub_value/100000000,2) “subValueFee”, round(b.sub_value*100,2) “subValueRate” from ( select distinct ORG_SHORT_NAME proSimple,
d.sub_value from k_dim_rela_a o, DM_IGW_MON_INDEX_VALUE_INFO d where o.dim_code = ‘80000002’ and o.dim_value = d.org_no
and d.org_no != ‘11101’ and d.STAT_DATE = 202106 and d.sub_no = ‘M000008’ and d.weidu_type = 00
and d.weidu_value = 00) a
left join
(select distinct ORG_SHORT_NAME proSimple , d.sub_value from k_dim_rela_a o, DM_IGW_MON_INDEX_VALUE_INFO d where o.dim_code = ‘80000002’ and o.dim_value = d.org_no and d.org_no != ‘11101’ and d.STAT_DATE = 202106 and d.sub_no = ‘M000007’ and d.weidu_type = 00 and d.weidu_value = 00) b on a.PROSIMPLE = b.PROSIMPLE ORDER BY “subValueFee” desc

下面是优化的代码
SELECT ORG_SHORT_NAME “proSimple”,round(M000008/100000000, 2) “subValueFee”,
round(M000007*100, 2) “subValueRate” FROM ( SELECT * FROM ( SELECT A.SUB_NO,A.SUB_VALUE,b.ORG_SHORT_NAME
FROM DM_IGW_MON_INDEX_VALUE_INFO A, k_dim_rela_a b WHERE A.STAT_DATE = ‘202106’ and A .org_no = b.dim_value and b.top_dim_value = ‘11101’ AND b.dim_code = ‘80000002’)
PIVOT ( SUM (SUB_VALUE) FOR SUB_NO IN ( ‘M000008’ M000008, ‘M000007’ M000007))) ORDER BY M000008 DESC NULLS LAST
– ①先将条件和查找的结果字段都先查出来
– ②、外层用pivot分组
– ③、再外层对pivot查询的数据进行处理、起别名
–④、根据字段进行排序

标签:dim,sub,no,VALUE,代码优化,value,ing,SUB
来源: https://blog.csdn.net/weixin_54355236/article/details/120563510