保单件数分布统计.sql 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. CREATE TABLE shanglifeecif.bdnum_distribution(
  2. id string,
  3. labelName string DEFAULT NULL COMMENT '保单件数名称',
  4. bdnum string DEFAULT NULL COMMENT '保单件数'
  5. ) COMMENT '保单件数分布'
  6. STORED AS ES
  7. with shard number 10
  8. replication 1;
  9. CREATE OR REPLACE PROCEDURE shanglifeecif.bdnum_distribution() IS
  10. DECLARE
  11. BEGIN
  12. --第一步 更新 Individual.NOVPolicy 有效记录数
  13. UPDATE shanglifeecif.Individual si1 SET si1.NOVPolicy = (SELECT nvl(t.cnum,0) FROM (
  14. SELECT temp.customerno,count(*) AS cnum FROM (
  15. SELECT max(p.customerno) customerno,p.contno FROM dsj.policy_information p WHERE p.appflag = '有效' GROUP BY p.contno
  16. ) temp GROUP BY temp.customerno
  17. ) t WHERE t.customerno = si1.scustid);
  18. --第二步 更新 Individual.NOIVPolicy 无效记录数
  19. UPDATE shanglifeecif.Individual si1 SET si1.noivpolicy = (SELECT nvl(t.cnum,0) FROM (
  20. SELECT temp.customerno,count(*) AS cnum FROM (
  21. SELECT max(p.customerno) customerno,p.contno FROM dsj.policy_information p WHERE p.appflag <> '有效' GROUP BY p.contno
  22. ) temp GROUP BY temp.customerno
  23. ) t WHERE t.customerno = si1.scustid);
  24. --第三步
  25. UPDATE shanglifeecif.Individual si1 SET si1.Label25 = (
  26. CASE
  27. WHEN (nvl(si1.NOVPolicy,0) + nvl(si1.NOIVPolicy,0)) == 0 THEN '无保单'
  28. WHEN (nvl(si1.NOVPolicy,0) + nvl(si1.NOIVPolicy,0)) == 1 THEN '1件保单'
  29. WHEN (nvl(si1.NOVPolicy,0) + nvl(si1.NOIVPolicy,0)) == 2 THEN '2件保单'
  30. WHEN (nvl(si1.NOVPolicy,0) + nvl(si1.NOIVPolicy,0)) >= 3 AND (nvl(si1.NOVPolicy,0) + nvl(si1.NOIVPolicy,0)) <= 5 THEN '3-5件保单'
  31. WHEN (nvl(si1.NOVPolicy,0) + nvl(si1.NOIVPolicy,0)) >= 6 AND (nvl(si1.NOVPolicy,0) + nvl(si1.NOIVPolicy,0)) <= 10 THEN '6-10件保单'
  32. WHEN (nvl(si1.NOVPolicy,0) + nvl(si1.NOIVPolicy,0)) >= 11 AND (nvl(si1.NOVPolicy,0) + nvl(si1.NOIVPolicy,0))<= 20 THEN '11-20件保单'
  33. WHEN (nvl(si1.NOVPolicy,0) + nvl(si1.NOIVPolicy,0)) >= 21 AND (nvl(si1.NOVPolicy,0) + nvl(si1.NOIVPolicy,0)) <= 50 THEN '21-50件保单'
  34. WHEN (nvl(si1.NOVPolicy,0) + nvl(si1.NOIVPolicy,0)) > 50 THEN '50件保单以上'
  35. END
  36. ) where si1.custtype like '%投保人%';
  37. DELETE FROM shanglifeecif.bdnum_distribution;
  38. insert into shanglifeecif.bdnum_distribution (
  39. id,
  40. labelName,
  41. bdnum
  42. ) SELECT row_number()over(),t.labelName,t.bdnum FROM (
  43. SELECT si.label25 labelName,count(1) bdnum FROM shanglifeecif.individual si GROUP BY si.label25
  44. ) t;
  45. EXCEPTION
  46. WHEN HIVE_EXCEPTION THEN
  47. INSERT INTO shanglifeecif.exception_log(log_id,log_code,log_msg,log_time) VALUES (to_char(sysdate(),'yyyyMMddHHmmss'),sqlcode(),sqlerrm(),sysdate());
  48. WHEN Others THEN
  49. INSERT INTO shanglifeecif.exception_log(log_id,log_code,log_msg,log_time) VALUES (to_char(sysdate(),'yyyyMMddHHmmss'),sqlcode(),sqlerrm(),sysdate());
  50. END ;