用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - sql代码库

客户区域调整

2015-05-27 作者: joey_hjy举报

[sql]代码库

DROP TABLE IT_HEJY.T_CUST_RECLAIM_TEMP;
CREATE TABLE IT_HEJY.T_CUST_RECLAIM_TEMP
(CUST_ID NUMBER,
 STATE NUMBER,
 CUSTOM_TYPE_ID NUMBER,
 OLD_AREA_ID NUMBER,
 OLD_AREA_NAME VARCHAR2(128),
 OLD_AREA_CNT NUMBER DEFAULT 0,
 OLD_AREA_CNT_ZW NUMBER DEFAULT 0,
 OLD_AREA_CNT_C NUMBER DEFAULT 0,
 OLD_AREA_CNT_G NUMBER DEFAULT 0,
 OLD_AREA_CNT_QT NUMBER DEFAULT 0,
 NEW_AREA_ID NUMBER DEFAULT 0,
 NEW_AREA_NAME VARCHAR2(128),
 NEW_AREA_CNT NUMBER DEFAULT 0,
 NEW_AREA_CNT_ZW NUMBER DEFAULT 0,
 NEW_AREA_CNT_C NUMBER DEFAULT 0,
 NEW_AREA_CNT_G NUMBER DEFAULT 0,
 NEW_AREA_CNT_QT NUMBER DEFAULT 0,
 SERV_OWE NUMBER DEFAULT 0,
 ACCT_OWE NUMBER DEFAULT 0
);

DELETE FROM IT_HEJY.T_CUST_RECLAIM_TEMP;
INSERT INTO IT_HEJY.T_CUST_RECLAIM_TEMP(CUST_ID,STATE,CUSTOM_TYPE_ID,OLD_AREA_ID,OLD_AREA_NAME,OLD_AREA_CNT,OLD_AREA_CNT_ZW,
OLD_AREA_CNT_C,OLD_AREA_CNT_G,OLD_AREA_CNT_QT)
SELECT A.CUST_ID,A.STATE,A.CUSTOM_TYPE_ID,C.AREA_ID,C.AREA_NAME,
COUNT(CASE WHEN A.LOCAL_AREA_ID=B.LOCAL_AREA_ID THEN B.SERV_ID ELSE NULL END) CNT,
COUNT(CASE WHEN A.LOCAL_AREA_ID=B.LOCAL_aREA_ID AND B.STATE<>'F1X' THEN B.SERV_ID ELSE NULL END) CNT_ZW,
COUNT(CASE WHEN A.LOCAL_AREA_ID=B.LOCAL_aREA_ID AND B.PROD_TYPE='CDMA' AND B.STATE<>'F1X' THEN B.SERV_ID ELSE NULL END) CNT_C,
COUNT(CASE WHEN A.LOCAL_AREA_ID=B.LOCAL_aREA_ID AND B.PROD_TYPE IN ('GH','KD','ITV') AND B.STATE<>'F1X' THEN B.SERV_ID ELSE NULL END) CNT_G,
COUNT(CASE WHEN A.LOCAL_AREA_ID=B.LOCAL_aREA_ID AND B.PROD_TYPE NOT IN ('CDMA','GH','KD','ITV') AND B.STATE<>'F1X' THEN B.SERV_ID ELSE NULL END) CNT_QT
  FROM SXDM.SXBD_CUST_AREA_LOCK_201504 A
 INNER JOIN IT_HEJY.SXBD_SERV_MSG_MON_201504 B
 ON A.CUST_ID=B.CUST_ID
 INNER JOIN SXDM.SXMART_AREA_C5_SL C
 ON A.LOCAL_AREA_ID=C.AREA_ID
 WHERE A.LOCAL_AREA_ID IN (33292, 33284, 33293, 33281, 33283, 33285, 33294)
   AND A.STATE IN (1, 2, -9)
 GROUP BY A.CUST_ID,A.STATE,A.CUSTOM_TYPE_ID,C.AREA_ID,C.AREA_NAME;
 
MERGE INTO IT_HEJY.T_CUST_RECLAIM_TEMP  A
USING (
SELECT A.*,ROW_NUMBER() OVER(PARTITION BY A.CUST_ID ORDER BY C.LVL DESC,A.CNT_ZW DESC,A.CNT_G DESC,A.CNT_C DESC,A.CNT DESC) RNUM
FROM ( 
SELECT A.CUST_ID,A.STATE,A.CUSTOM_TYPE_ID,C.AREA_ID,C.AREA_NAME,COUNT(B.SERV_ID) CNT,COUNT(DECODE(B.STATE,'F1X',NULL,B.SERV_ID)) CNT_ZW,
COUNT(CASE WHEN B.PROD_TYPE='CDMA' AND B.STATE<>'F1X' THEN B.SERV_ID ELSE NULL END) CNT_C,
COUNT(CASE WHEN B.PROD_TYPE IN ('GH','KD','ITV') AND B.STATE<>'F1X' THEN B.SERV_ID ELSE NULL END) CNT_G,
COUNT(CASE WHEN B.PROD_TYPE NOT IN ('CDMA','GH','KD','ITV') AND B.STATE<>'F1X' THEN B.SERV_ID ELSE NULL END) CNT_QT
  FROM SXDM.SXBD_CUST_AREA_LOCK_201504 A
 INNER JOIN IT_HEJY.SXBD_SERV_MSG_MON_201504 B
 ON A.CUST_ID=B.CUST_ID
 INNER JOIN SXDM.SXMART_AREA_C5_SL C
 ON B.LOCAL_AREA_ID=C.AREA_ID
 INNER JOIN SXDM.SXMART_AREA_C5_SL D
 ON A.LOCAL_aREA_ID=D.AREA_ID
 WHERE A.LOCAL_AREA_ID IN (33292, 33284, 33293, 33281, 33283, 33285, 33294)
   AND A.STATE IN (1, 2, -9)
   AND C.AREA_ID<>D.AREA_ID
   AND C.C5_AREA_ID=D.C5_AREA_ID
 GROUP BY A.CUST_ID,A.STATE,A.CUSTOM_TYPE_ID,C.AREA_ID,C.AREA_NAME) A) B
 ON (A.CUST_ID=B.CUST_ID AND B.RNUM=1)
 WHEN MATCHED THEN
   UPDATE SET A.NEW_AREA_ID=B.AREA_ID,A.NEW_AREA_NAME=B.AREA_NAME,A.NEW_AREA_CNT=B.CNT,A.NEW_aREA_CNT_ZW=B.CNT_ZW,
   A.NEW_AREA_CNT_C=B.CNT_C,A.NEW_AREA_CNT_G=B.CNT_G,A.NEW_AREA_CNT_QT=B.CNT_QT;


网友评论    (发表评论)


发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...