age_sex_distribution.sql 1.3 KB

123456789101112131415161718192021
  1. CREATE OR REPLACE PROCEDURE shanglifeecif.age_sex_distribution() IS
  2. DECLARE
  3. BEGIN
  4. 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);
  5. DELETE FROM shanglifeecif.age_sex_distribution;
  6. insert into shanglifeecif.age_sex_distribution (
  7. id,
  8. labelName,
  9. gender,
  10. cusNum
  11. ) SELECT row_number()over(),t.labelName,t.gender,t.cusNum FROM (
  12. SELECT si.label4 labelName,si.gender,count(1) cusNum FROM shanglifeecif.individual si WHERE si.label4 IS NOT null GROUP BY si.label4,si.gender
  13. ) t;
  14. dbms_output.put_line('age_sex_distribution函数跑批完成!');
  15. EXCEPTION
  16. WHEN HIVE_EXCEPTION THEN
  17. INSERT INTO shanglifeecif.exception_log(log_id,log_code,log_msg,log_time) VALUES (to_char(sysdate(),'yyyyMMddHHmmss'),sqlcode(),sqlerrm(),sysdate());
  18. WHEN Others THEN
  19. INSERT INTO shanglifeecif.exception_log(log_id,log_code,log_msg,log_time) VALUES (to_char(sysdate(),'yyyyMMddHHmmss'),sqlcode(),sqlerrm(),sysdate());
  20. END ;