年龄段性别分布统计.sql 1.5 KB

123456789101112131415161718192021222324252627282930
  1. CREATE TABLE shanglifeecif.age_sex_distribution(
  2. id string DEFAULT NULL,
  3. labelname string DEFAULT NULL COMMENT '阶段名称',
  4. gender string DEFAULT NULL COMMENT '性别',
  5. cusnum string DEFAULT NULL COMMENT '客户数量'
  6. )
  7. COMMENT '年龄段性别分布'
  8. STORED AS ES
  9. with shard number 10
  10. replication 1;
  11. CREATE OR REPLACE PROCEDURE shanglifeecif.age_sex_distribution() IS
  12. DECLARE
  13. BEGIN
  14. UPDATE shanglifeecif.Individual si1 SET si1.label4 = (SELECT CASE WHEN temp.age>=0 AND temp.age<13 THEN '儿童' WHEN temp.age>=13 AND temp.age <19 THEN '少年' WHEN temp.age >=19 AND temp.age <41 THEN '青年' WHEN temp.age >=41 AND temp.age <66 THEN '中年' ELSE '老年' END FROM (SELECT TRUNC(months_between(sysdate, si2.birthday)/12) age,si2.indid FROM shanglifeecif.Individual si2) temp WHERE temp.indid = si1.indid);
  15. DELETE FROM shanglifeecif.age_sex_distribution;
  16. insert into shanglifeecif.age_sex_distribution (
  17. id,
  18. labelName,
  19. gender,
  20. cusNum
  21. ) SELECT row_number()over(),t.labelName,t.gender,t.cusNum FROM (
  22. SELECT si.label4 labelName,si.gender,count(1) cusNum FROM shanglifeecif.individual si GROUP BY si.label4,si.gender
  23. ) t;
  24. EXCEPTION
  25. WHEN HIVE_EXCEPTION THEN
  26. INSERT INTO shanglifeecif.exception_log(log_id,log_code,log_msg,log_time) VALUES (to_char(sysdate(),'yyyyMMddHHmmss'),sqlcode(),sqlerrm(),sysdate());
  27. WHEN Others THEN
  28. INSERT INTO shanglifeecif.exception_log(log_id,log_code,log_msg,log_time) VALUES (to_char(sysdate(),'yyyyMMddHHmmss'),sqlcode(),sqlerrm(),sysdate());
  29. END ;