How to transpose these extra job_title rows into their own columns?

  Kiến thức lập trình

software im using is snowflake

I have a table of an employees job assignments with the company. They can hold multiple jobs actively at once (null in end_date). I’d like to consolidate this into 1 row:

EMPLOYEE_ID START_DATE END_DATE JOB_TITLE
1442 7/30/24 Tutor
1442 7/30/24 Tutor
1442 6/28/24 Instructional Specialist
1442 5/1/24 6/27/24 Instructional Specialist
1442 12/16/21 7/29/24 Tutor
1442 12/16/21 Lead Instructor
1442 12/16/21 7/29/24 Tutor

If an employee has any null values in the end_date field, then Id like to only retrieve distinct job_titles (eliminate top 2 rows to 1 because both same job on same start date)
1-5 in desc order based on start_date like this:

EMPLOYEE_ID Job_Title_1 Job_Title_2 Job_Title_3 Job_Title_4 Job_Title_5
1442 Tutor Instructional Specialist Lead Instructor

now lets say this employee had no currently active jobs, the table would look like this:

EMPLOYEE_ID START_DATE END_DATE JOB_TITLE
1442 5/1/24 6/27/24 Instructional Specialist
1442 12/16/21 7/29/24 Tutor
1442 12/16/21 7/29/24 Tutor

in that case I’d like the table to look like this:

EMPLOYEE_ID Job_Title_1 Job_Title_2 Job_Title_3 Job_Title_4 Job_Title_5
1442 Instructional Specialist Tutor

Here is the query I am using, and it works, but it’s not ordering the job_title 1-5 columns by desc start_date order:

WITH job_position_info_ADP AS (
    SELECT 
        'ADP' AS source, 
        CAST(w.associate_oid AS STRING) AS worker_id,
        CAST(w.id AS STRING) AS Employee_ID,
        TO_CHAR(wah._fivetran_start, 'MM/DD/YY') AS start_date,
        CASE 
            WHEN wah._fivetran_active = TRUE THEN NULL
            ELSE TO_CHAR(wah._fivetran_end, 'MM/DD/YY')
        END AS end_date,
        wah.job_title AS Job_Title,
        ROW_NUMBER() OVER (PARTITION BY CAST(w.id AS STRING) ORDER BY wah._fivetran_start DESC) AS rn
    FROM
        prod_raw.adp_workforce_now.worker w
    JOIN 
        prod_raw.adp_workforce_now.worker_report_to AS wr 
        ON w.id = wr.worker_id
    JOIN 
        prod_raw.adp_workforce_now.work_assignment_history AS wah 
        ON w.id = wah.worker_id
),
recent_jobs_with_null_end AS (
    SELECT 
        Employee_ID,
        Job_Title,
        ROW_NUMBER() OVER (PARTITION BY Employee_ID ORDER BY start_date DESC) AS rn
    FROM 
        job_position_info_ADP
    WHERE 
        end_date IS NULL
),
recent_jobs_all AS (
    SELECT 
        Employee_ID,
        Job_Title,
        ROW_NUMBER() OVER (PARTITION BY Employee_ID ORDER BY start_date DESC) AS rn
    FROM 
        job_position_info_ADP
)
SELECT
    Employee_ID,
    MAX(CASE WHEN rn = 1 THEN Job_Title END) AS Job_Title_1,
    MAX(CASE WHEN rn = 2 THEN Job_Title END) AS Job_Title_2,
    MAX(CASE WHEN rn = 3 THEN Job_Title END) AS Job_Title_3,
    MAX(CASE WHEN rn = 4 THEN Job_Title END) AS Job_Title_4,
    MAX(CASE WHEN rn = 5 THEN Job_Title END) AS Job_Title_5
FROM (
    SELECT * FROM recent_jobs_with_null_end
    UNION ALL
    SELECT * FROM recent_jobs_all
    WHERE Employee_ID NOT IN (SELECT Employee_ID FROM recent_jobs_with_null_end)
) AS combined
WHERE 
    Employee_ID = '1442'
GROUP BY
    Employee_ID;

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT