最终效果
原始数据:
转换脚本:
WITH post_a AS ( SELECT DISTINCT T.EMP_NO,S.CODE_ FROM inscndb.DTFMA000_EMP_POST T ,VIEW_BI_POST S WHERE T.POST||'-'||POST2 = S.POST ), post_b AS (SELECT emp_no,code_,ROW_NUMBER() OVER(PARTITION BY EMP_NO ORDER BY EMP_NO) row_num FROM post_a), post_c(emp_no,code_,row_num) AS (SELECT post_b.emp_no,CAST(post_b.code_ AS VARCHAR(100)) AS code_,post_b.row_num FROM post_b WHERE post_b.row_num = 1 UNION ALL SELECT post_b.emp_no, CAST( post_c.code_|| ',' || post_b.code_ AS varchar(100)) AS code_,post_b.row_num FROM post_b,post_c WHERE post_b.row_num=(post_c.row_num+1) AND post_b.emp_no = post_c.emp_no ) SELECT c.emp_no,c.code_,c.row_num FROM post_c c WHERE NOT EXISTS (SELECT 1 FROM post_c cc WHERE c.emp_no = cc.emp_no AND c.row_num < cc.row_num) ORDER BY c.emp_no ;