SELECTCITYCODE,ZONE,SUM(LIVELYUSERS)ASLIVELYUSERS,SUM(RENZHENGUSERS)ASRENZHENGUSERS,SUM(DULIUSERS)ASDULIUSERSFROM(SELECTCASEWHENT.CITYCODEISNULLANDT.ADSLACCISNOTNULLTHEN'...
SELECT CITYCODE,ZONE,SUM(LIVELYUSERS) AS LIVELYUSERS,SUM(RENZHENGUSERS) AS RENZHENGUSERS,
SUM(DULIUSERS) AS DULIUSERS
FROM(
SELECT CASE
WHEN T.CITYCODE IS NULL AND T.ADSLACC IS NOT NULL THEN
'其他(未知地市)'
WHEN T.CITYCODE IS NULL AND T.ADSLACC IS NULL THEN
'未认证'
ELSE
S.CITIESNAME
END AS ZONE,
T.CITYCODE,
COUNT(T.MUSERID) AS LIVELYUSERS,
COUNT(CASE WHEN T.ADSLACC IS NOT NULL THEN T.ADSLACC END) AS RENZHENGUSERS,
COUNT(DISTINCT CASE WHEN T.ADSLACC IS NOT NULL THEN T.ADSLACC END) AS DULIUSERS
FROM TBL_USERINFO_JX T
LEFT JOIN SERCITIESINFOTABLE S ON T.CITYCODE = S.CITIESID
INNER JOIN (SELECT *
FROM (SELECT T1.YUSERID,
T1.FDATE,
T1.MUSERID,
ROW_NUMBER() OVER(PARTITION BY T1.MUSERID ORDER BY T1.FDATE DESC) RN
FROM TBL_GETUSERINFO_JX T1
WHERE SUBSTR(T1.CHANNEL,
INSTR(T1.CHANNEL, '-') + 1,
INSTR(T1.CHANNEL, '-') - 2) = '15' and TO_CHAR(T1.FDATE,'YYYY-MM-DD')>='2012-01-30' AND TO_CHAR(T1.FDATE,'YYYY-MM-DD')<='2012-02-01')
WHERE RN = 1) T2 ON T.MUSERID=T2.MUSERID
GROUP BY T.CITYCODE,S.CITIESNAME,T.ADSLACC) GROUP BY ZONE,CITYCODE ORDER BY CITYCODE ASC;
谁能帮忙优化下这sql,统计上10w条记录,速度非常慢,
展开