谁来帮忙精简下此条SQL,长度超过4000了

huyou1983218 2010-11-20 06:28:17

Select Top 11 cell_cn_name FROM (select top 11 Row_Number()
over(order by incr_value desc) as rownum1 ,* from ( select top 5
Row_Number() over(order by incr_value desc) as
rownum,replace(replace(replace(replace(replace
(replace(replace(replace(replace(replace(replace
(replace(cell_cn_name,'0',''),'1',''),'2',''),'3',''),'4',''),'5',''),'6',''),'7',''),'8',''),'9',''),'.',''),' | 各项贷款','') as cell_cn_name, round(incr_value/10000,2) as
incr_value,rept_year,rept_month,org_id,data_range_id,cur_id,freq_id
from (select cell_cn_name,cast(isnull(curr_term_value,0) as float)
- cast(isnull(prev_term_value,0)as float) as incr_value,
rept_year,rept_month,org_id,data_range_id,cur_id,freq_id from
( select t1.cell_cn_name,rd.curr_term_value,
rd.rept_year,rd.rept_month,rd.org_id,rd.data_range_id,rd.cur_id,
rd.freq_id ,(select curr_term_value from
report_data d where d.cell_index=rd.cell_index and d.rept_year=dbo.getlastyear(2,9,2010,1) and
d.rept_month=dbo.getlastmonth(2,9,1) and d.cur_id=rd.cur_id and
d.freq_id=rd.freq_id and (d.status='1' or d.status is null) ) as
prev_term_value from report_data rd, (select
cell_index,cell_cn_name,version_id from cell_info where
template_id='G1101' and col_id='C' and ((row_id>=10 and row_id<=29)
or row_id=35 or row_id=36)) t1 where rd.cell_index=t1.cell_index
and rd.version_id=t1.version_id and rd.rept_year=2010 and rd.rept_month=9 and rd.org_id='F093H101510101001' and
rd.data_range_id=1 and rd.cur_id=1 and (rd.status='1'
or rd.status is null) ) t2) as tmpDR union all select top 5 Row_Number() over(order by incr_value asc) as rownum,replace(replace(replace(replace(replace(replace
(replace(replace(replace(replace(replace(replace
(cell_cn_name,'0',''),'1',''),'2',''),'3',''),'4',''),'5',''),'6','')
,'7',''),'8',''),'9',''),'.',''),' | 各项贷款','') as cell_cn_name,
round(incr_value/10000,2) as
incr_value,rept_year,rept_month,org_id,data_range_id,
cur_id,freq_id from (select cell_cn_name,cast(isnull(curr_term_value,0) as float) - cast(isnull(prev_term_value,0)as float) as incr_value,
rept_year,rept_month,org_id,data_range_id,
cur_id,freq_id from ( select t1.cell_cn_name,rd.curr_term_value, rd.rept_year,rd.rept_month,rd.org_id,rd.data_range_id,
rd.cur_id,rd.freq_id ,(select curr_term_value from report_data d where d.cell_index=rd.cell_index and d.rept_year=dbo.getlastyear(2,9,2010,1) and d.rept_month=dbo.getlastmonth(2,9,1) and
d.cur_id=rd.cur_id and d.freq_id=rd.freq_id and (d.status='1' or d.status is null) ) as prev_term_value
from report_data rd,
(select cell_index,cell_cn_name,version_id from cell_info where template_id='G1101' and col_id='C' and ((row_id>=10 and row_id<=29) or row_id=35 or row_id=36)) t1
where rd.cell_index=t1.cell_index and rd.version_id=t1.version_id and rd.rept_year=2010 and rd.rept_month=9 and rd.org_id='F093H101510101001'
and rd.data_range_id=1 and rd.cur_id=1 and (rd.status='1' or rd.status is null) ) t2) as tmpDR
union all
select 6,'行业平均' as cell_cn_name ,
avg(incr_value),rept_year,rept_month,org_id,data_range_id,cur_id,freq_id from (select cell_cn_name,cast(isnull(curr_term_value,0) as float) - cast(isnull(prev_term_value,0)as float) as incr_value,
rept_year,rept_month,org_id,data_range_id,cur_id,
freq_id from ( select t1.cell_cn_name,rd.curr_term_value, rd.rept_year,rd.rept_month,rd.org_id,rd.data_range_id,
rd.cur_id,rd.freq_id ,(select curr_term_value from report_data d where d.cell_index=rd.cell_index and
d.rept_year=dbo.getlastyear(2,9,2010,1) and
d.rept_month=dbo.getlastmonth(2,9,1) and d.cur_id=rd.cur_id and
d.freq_id=rd.freq_id and (d.status='1' or d.status is null) ) as
prev_term_value from report_data rd, (select
cell_index,cell_cn_name,version_id from cell_info where
template_id='G1101' and col_id='C' and ((row_id>=10
and row_id<=29) or row_id=35 or row_id=36)) t1 where rd.cell_index=t1.cell_index and rd.version_id=t1.version_id and
rd.rept_year=2010 and rd.rept_month=9 and rd.org_id='F093H101510101001' and rd.data_range_id=1
and rd.cur_id=1 and (rd.status='1' or rd.status is null) ) t2)as
tmpDR group by rept_year,rept_month,org_id,data_range_id,cur_id,freq_id )
temp order by rownum1 ) as t where Org_ID='F093H101510101001' AND Rept_Year=2010 AND Rept_Month=9 AND
Data_Range_ID=1 AND Cur_ID=1 AND Freq_ID<=2

...全文
106 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
王向飞 2010-11-20
  • 打赏
  • 举报
回复
-晴天 2010-11-20
  • 打赏
  • 举报
回复
;with cte as(
select replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(cell_cn_name,'0',''),'1',''),'2',''),'3',''),'4',''),'5',''),'6',''),'7',''),'8',''),'9',''),'.',''),' | 各项贷款','') as cell_cn_name,
cast(isnull(curr_term_value,0) as float) - cast(isnull(prev_term_value,0)as float) as incr_value,
rept_year,rept_month,org_id,data_range_id,cur_id,freq_id
from (
select t1.cell_cn_name,rd.curr_term_value, rd.rept_year,rd.rept_month,rd.org_id,rd.data_range_id,rd.cur_id,rd.freq_id ,
(select curr_term_value from report_data d where d.cell_index=rd.cell_index and d.rept_year=dbo.getlastyear(2,9,2010,1) and
d.rept_month=dbo.getlastmonth(2,9,1) and d.cur_id=rd.cur_id and d.freq_id=rd.freq_id and (d.status='1' or d.status is null) ) as prev_term_value
from report_data rd, (
select cell_index,cell_cn_name,version_id
from cell_info where template_id='G1101' and col_id='C' and ((row_id>=10 and row_id<=29) or row_id=35 or row_id=36)
) t1
where rd.cell_index=t1.cell_index and rd.version_id=t1.version_id and rd.rept_year=2010 and rd.rept_month=9
and rd.org_id='F093H101510101001' and rd.data_range_id=1 and rd.cur_id=1 and (rd.status='1' or rd.status is null) and Freq_ID<=2
) t2
)
Select cell_cn_name FROM (
select top 11 Row_Number() over(order by incr_value desc) as rownum1 ,* from (
select top 5 Row_Number() over(order by incr_value desc) as rownum,cell_cn_name,round(incr_value/10000,2) as incr_value,rept_year,rept_month,org_id,data_range_id,cur_id,freq_id
from cte
union all
select top 5 Row_Number() over(order by incr_value asc) as rownum,cell_cn_name,round(incr_value/10000,2) as incr_value,rept_year,rept_month,org_id,data_range_id,cur_id,freq_id
from cte
union all
select 6,'行业平均' as cell_cn_name ,avg(incr_value),rept_year,rept_month,org_id,data_range_id,cur_id,freq_id
from cte
group by rept_year,rept_month,org_id,data_range_id,cur_id,freq_id
) temp order by rownum1
) as t
--http://topic.csdn.net/u/20101120/18/c3863270-c969-47f9-b772-a2b7fb2ecc7d.html?56446

xyytuo 2010-11-20
  • 打赏
  • 举报
回复
coleling 2010-11-20
  • 打赏
  • 举报
回复
在不清楚具体结构的情况下,只能做到这一步了

With t1 as
(
select cell_index,cell_cn_name,version_id
from cell_info
where template_id='G1101' and col_id='C' and ((row_id>=10 and row_id<=29) or row_id=35 or row_id=36)
)

,t2 as
(
select t1.cell_cn_name,rd.curr_term_value, rd.rept_year,rd.rept_month,
rd.org_id,rd.data_range_id,rd.cur_id,rd.freq_id ,(
select curr_term_value from report_data d
where d.cell_index=rd.cell_index and d.rept_year=dbo.getlastyear(2,9,2010,1) and
d.rept_month=dbo.getlastmonth(2,9,1) and d.cur_id=rd.cur_id and
d.freq_id=rd.freq_id and (d.status='1' or d.status is null)
) as prev_term_value
from report_data rd, t1
where rd.cell_index=t1.cell_index and rd.version_id=t1.version_id and
rd.rept_year=2010 and rd.rept_month=9 and rd.org_id='F093H101510101001' and
rd.data_range_id=1 and rd.cur_id=1 and (rd.status='1' or rd.status is null)
)

,tmpDR as
(
select cell_cn_name,
cast(isnull(curr_term_value,0) as float) - cast(isnull(prev_term_value,0)as float) as incr_value,
rept_year,rept_month,org_id,data_range_id,cur_id,freq_id
from t2
)

--将内外的Top 11都去掉了,因为5+5+1最多就11条,不需要再加
Select cell_cn_name
FROM (
select Row_Number() over(order by incr_value desc) as rownum1 ,*
from (
select top 5 Row_Number() over(order by incr_value desc) as rownum,
replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(
replace(replace(cell_cn_name,'0',''),'1',''),'2',''),'3',''),'4',''),'5',''),'6','')
,'7',''),'8',''),'9',''),'.',''),' | 各项贷款','') as cell_cn_name,
round(incr_value/10000,2) as incr_value,rept_year,rept_month,org_id,data_range_id,cur_id,freq_id
from tmpDR

union all

select top 5 Row_Number() over(order by incr_value asc) as rownum,
replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(
replace(replace(cell_cn_name,'0',''),'1',''),'2',''),'3',''),'4',''),'5',''),'6','')
,'7',''),'8',''),'9',''),'.',''),' | 各项贷款','') as cell_cn_name,
round(incr_value/10000,2) as incr_value,rept_year,rept_month,org_id,data_range_id,cur_id,freq_id
from tmpDR

union all

select 6,'行业平均' as cell_cn_name ,
avg(incr_value),rept_year,rept_month,org_id,data_range_id,cur_id,freq_id
from tmpDR

group by rept_year,rept_month,org_id,data_range_id,cur_id,freq_id
) temp
) as t
where Org_ID='F093H101510101001' AND Rept_Year=2010 AND Rept_Month=9 AND Data_Range_ID=1 AND Cur_ID=1 AND Freq_ID<=2
order by rownum1

其中的Replace可以用自定义函数解决。

最后,拜一下本语句的原创者,太强大了
叶子 2010-11-20
  • 打赏
  • 举报
回复
替换0123456789.可以用正则,可以用自定义函数。
-晴天 2010-11-20
  • 打赏
  • 举报
回复
第二级查询中已经有了 top 11,主查询就不需要用了.
-晴天 2010-11-20
  • 打赏
  • 举报
回复
最简单的:每个联合的子查询里都有Org_ID='F093H101510101001',那主查询语句里还要它干嘛?
-晴天 2010-11-20
  • 打赏
  • 举报
回复
有好多内容都是重复的.
chen8410 2010-11-20
  • 打赏
  • 举报
回复
这么长的代码,至少先把格式调一下也好读
最好提供点测试数据
飘零一叶 2010-11-20
  • 打赏
  • 举报
回复
子查询太多了....
飘零一叶 2010-11-20
  • 打赏
  • 举报
回复

飘零一叶 2010-11-20
  • 打赏
  • 举报
回复

DataBox-MDX 2010-11-20
  • 打赏
  • 举报
回复
神啊!牛人

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧