问答详情
源自:-

sql优化怎么做的?

这条语句还能怎么优化?大神们帮帮忙吧!谢谢

SELECT

a.*,

CASE

WHEN a.distno = '01' THEN

concat( DD.distname, '-合计' ) ELSE DD.distname 

END distname,

cast( ztyes/zts * 100 AS DECIMAL ( 8, 2 ) ) wcl,

2017 nf,

1 yf,

'' lx 

FROM

(

SELECT

ifnull( distno, '01' ) distno,

zts,

ztyes,

czs,

qyzs,

bfh 

FROM

(

SELECT

CASE

WHEN

( '01' ) = ( 4+3 ) THEN

a.linkDistId ELSE substring( a.linkDistId, 1, 4 ) 

END distno,

count( DISTINCT a.acc_set ) zts,

count( DISTINCT b.acc_set ) ztyes,

count( DISTINCT CASE WHEN a.acc_style = '经济帐' THEN b.acc_set END ) czs,

count( DISTINCT CASE WHEN a.acc_style = '企业帐' THEN b.acc_set END ) qyzs,

0 bfh 

FROM

gz_acc_set a

LEFT OUTER JOIN gz_balance b ON a.acc_set = b.acc_set 

AND b.years = 2017 

AND b.months = 1 

WHERE

a.dwlx LIKE

CASE

WHEN '' = '' 

OR '' = '全部' THEN

'%' ELSE '' 

END 

AND a.linkDistId LIKE '01%' 

GROUP BY

CASE

WHEN ( '01' ) = ( 4+3 ) THEN

a.linkDistId ELSE substring( a.linkDistId, 1, 4 ) 

END WITH ROLLUP 

) a 

) a,

( SELECT distno, distname FROM scandist ) DD 

WHERE

DD.distno = a.distno


提问者:潇湘_夜雨 2018-04-19 15:18

个回答