求教各位大神,下面的陳述句能否簡化優化?感覺這樣寫很死啊
(select '2017-3-1' yf, b.*,a.*,case zbsl when 0 then 'ERROR' else to_char(round((zbsl-fbsl)/zbsl*100,2),'fm999999990.999999999')||'%' end cxcl from
(select count(distinct a.hh ) zbl,sum(b.scsl) zbsl from da_yhbk a,cb_slb_ls b,da_glszb c
where a.hh=c.hh and a.hh=b.hh and b.sfyf between add_months(date'2017-3-1',-2) and date'2017-3-1'
and c.qybm='qqqq'
) a,
(select count(distinct a.hh) fbl,sum(b.scsl) fbsl from da_yhbk a,cb_slb_ls b
where slqy='qqqq' and a.hh=b.hh and b.sfyf between add_months(date'2017-3-1',-2) and date'2017-3-1'
) b
where 1=1 )
union all
(select '2017-6-1' yf, b.*,a.*,case zbsl when 0 then 'ERROR' else to_char(round((zbsl-fbsl)/zbsl*100,2),'fm999999990.999999999')||'%' end cxcl from
(select count(distinct a.hh ) zbl,sum(b.scsl) zbsl from da_yhbk a,cb_slb_ls b,da_glszb c
where a.hh=c.hh and a.hh=b.hh and b.sfyf between add_months(date'2017-6-1',-2) and date'2017-6-1'
and c.qybm='qqqq'
) a,
(select count(distinct a.hh) fbl,sum(b.scsl) fbsl from da_yhbk a,cb_slb_ls b
where slqy='qqqq' and a.hh=b.hh and b.sfyf between add_months(date'2017-6-1',-2) and date'2017-6-1'
) b
where 1=1 )
union all
(select '2017-9-1' yf, b.*,a.*,case zbsl when 0 then 'ERROR' else to_char(round((zbsl-fbsl)/zbsl*100,2),'fm999999990.999999999')||'%' end cxcl from
(select count(distinct a.hh ) zbl,sum(b.scsl) zbsl from da_yhbk a,cb_slb_ls b,da_glszb c
where a.hh=c.hh and a.hh=b.hh and b.sfyf between add_months(date'2017-9-1',-2) and date'2017-9-1'
and c.qybm='qqqq'
) a,
(select count(distinct a.hh) fbl,sum(b.scsl) fbsl from da_yhbk a,cb_slb_ls b
where slqy='qqqq' and a.hh=b.hh and b.sfyf between add_months(date'2017-9-1',-2) and date'2017-9-1'
) b
where 1=1 )
union all
(select '2017-12-1' yf, b.*,a.*,case zbsl when 0 then 'ERROR' else to_char(round((zbsl-fbsl)/zbsl*100,2),'fm999999990.999999999')||'%' end cxcl from
(select count(distinct a.hh ) zbl,sum(b.scsl) zbsl from da_yhbk a,cb_slb_ls b,da_glszb c
where a.hh=c.hh and a.hh=b.hh and b.sfyf between add_months(date'2017-12-1',-2) and date'2017-12-1'
and c.qybm='qqqq'
) a,
(select count(distinct a.hh) fbl,sum(b.scsl) fbsl from da_yhbk a,cb_slb_ls b
where slqy='qqqq' and a.hh=b.hh and b.sfyf between add_months(date'2017-12-1',-2) and date'2017-12-1'
) b
where 1=1 )
uj5u.com熱心網友回復:
select yf, b.*,a.*,case zbsl when 0 then 'ERROR' else to_char(round((zbsl-fbsl)/zbsl*100,2),'fm999999990.999999999')||'%' end cxcl from
(select case when b.sfyf between add_months(date'2017-3-1',-2) and date'2017-3-1' then '2017-3-1'
when b.sfyf between add_months(date'2017-6-1',-2) and date'2017-6-1' then '2017-6-1'
when b.sfyf between add_months(date'2017-9-1',-2) and date'2017-9-1' then '2017-9-1'
when b.sfyf between add_months(date'2017-12-1',-2) and date'2017-12-1' then '2017-12-1'
end yf,
count(distinct a.hh ) zbl,sum(b.scsl) zbsl from da_yhbk a,cb_slb_ls b,da_glszb c
where a.hh=c.hh and a.hh=b.hh and b.sfyf between add_months(date'2017-3-1',-2) and date'2017-3-1'
and c.qybm='qqqq'
group by case when b.sfyf between add_months(date'2017-3-1',-2) and date'2017-3-1' then '2017-3-1'
when b.sfyf between add_months(date'2017-6-1',-2) and date'2017-6-1' then '2017-6-1'
when b.sfyf between add_months(date'2017-9-1',-2) and date'2017-9-1' then '2017-9-1'
when b.sfyf between add_months(date'2017-12-1',-2) and date'2017-12-1' then '2017-12-1'
end
) a,
(select case when b.sfyf between add_months(date'2017-3-1',-2) and date'2017-3-1' then '2017-3-1'
when b.sfyf between add_months(date'2017-6-1',-2) and date'2017-6-1' then '2017-6-1'
when b.sfyf between add_months(date'2017-9-1',-2) and date'2017-9-1' then '2017-9-1'
when b.sfyf between add_months(date'2017-12-1',-2) and date'2017-12-1' then '2017-12-1'
end yf,
count(distinct a.hh) fbl,sum(b.scsl) fbsl from da_yhbk a,cb_slb_ls b
where slqy='qqqq' and a.hh=b.hh and b.sfyf between add_months(date'2017-3-1',-2) and date'2017-3-1'
group by case when b.sfyf between add_months(date'2017-3-1',-2) and date'2017-3-1' then '2017-3-1'
when b.sfyf between add_months(date'2017-6-1',-2) and date'2017-6-1' then '2017-6-1'
when b.sfyf between add_months(date'2017-9-1',-2) and date'2017-9-1' then '2017-9-1'
when b.sfyf between add_months(date'2017-12-1',-2) and date'2017-12-1' then '2017-12-1'
end
) b
where 1=1 and a.yf = b.yf;
應該還可以優化下,這個你先看看吧,起碼陳述句可讀性好很多,減少了掃描表次數
uj5u.com熱心網友回復:
and b.sfyf between add_months(date'2017-3-1',-2) and date'2017-3-1' 有點問題,把where后面的去掉and b.sfyf between add_months(date'2017-3-1',-2) and date'2017-3-1'uj5u.com熱心網友回復:
看了下,a表和b表不能合并,因為不知道c表的資料是什么樣的,如果a和c匹配后出現了重復行,sum求和結果就會變大,不能合并。select yf, b.*,a.*,case zbsl when 0 then 'ERROR' else to_char(round((zbsl-fbsl)/zbsl*100,2),'fm999999990.999999999')||'%' end cxcl from
(select case when b.sfyf between add_months(date'2017-3-1',-2) and date'2017-3-1' then '2017-3-1'
when b.sfyf between add_months(date'2017-6-1',-2) and date'2017-6-1' then '2017-6-1'
when b.sfyf between add_months(date'2017-9-1',-2) and date'2017-9-1' then '2017-9-1'
when b.sfyf between add_months(date'2017-12-1',-2) and date'2017-12-1' then '2017-12-1'
end yf,
count(distinct a.hh ) zbl,sum(b.scsl) zbsl from da_yhbk a,cb_slb_ls b,da_glszb c
where a.hh=c.hh and a.hh=b.hh
and c.qybm='qqqq'
group by case when b.sfyf between add_months(date'2017-3-1',-2) and date'2017-3-1' then '2017-3-1'
when b.sfyf between add_months(date'2017-6-1',-2) and date'2017-6-1' then '2017-6-1'
when b.sfyf between add_months(date'2017-9-1',-2) and date'2017-9-1' then '2017-9-1'
when b.sfyf between add_months(date'2017-12-1',-2) and date'2017-12-1' then '2017-12-1'
end
) a,
(select case when b.sfyf between add_months(date'2017-3-1',-2) and date'2017-3-1' then '2017-3-1'
when b.sfyf between add_months(date'2017-6-1',-2) and date'2017-6-1' then '2017-6-1'
when b.sfyf between add_months(date'2017-9-1',-2) and date'2017-9-1' then '2017-9-1'
when b.sfyf between add_months(date'2017-12-1',-2) and date'2017-12-1' then '2017-12-1'
end yf,
count(distinct a.hh) fbl,sum(b.scsl) fbsl from da_yhbk a,cb_slb_ls b
where slqy='qqqq' and a.hh=b.hh
group by case when b.sfyf between add_months(date'2017-3-1',-2) and date'2017-3-1' then '2017-3-1'
when b.sfyf between add_months(date'2017-6-1',-2) and date'2017-6-1' then '2017-6-1'
when b.sfyf between add_months(date'2017-9-1',-2) and date'2017-9-1' then '2017-9-1'
when b.sfyf between add_months(date'2017-12-1',-2) and date'2017-12-1' then '2017-12-1'
end
) b
where 1=1 and a.yf = b.yf;
最終結果,1樓那個有點問題,忘了刪條件
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/102102.html
標籤:開發
下一篇:php 處理oracle 問題
