This is the input data:
enter image description here
I just want 1 instance for each work_contract.
I tried to use LIMIT 1 and used UNION ALL as well as UNION DISTINCT but it appears we can’t use LIMIT in UNION.
SELECT *,
FROM table1
where work_contract = "SECONDMENT"
LIMIT 1
UNION DISTINCT
SELECT *,
FROM table1
where work_contract = "INTER"
LIMIT 1
UNION DISTINCT
SELECT *,
FROM table1
where work_contract = "CASUAL"
LIMIT 1
This doesn’t work. Is there a way to choose only 1 instance at work_contract level?
>Solution :
you can use below query. I am assuming here you don’t want to include at employee id level and its just work_contract level.
SELECT *,
FROM table 1
where work_contract = "SECONDMENT"
qualify ( row_number() over (partition by work_contract order by employee_id,employee_type_description) = 1)
UNION DISTINCT
SELECT *,
FROM table 1
where work_contract = "INTER"
qualify ( row_number() over (partition by work_contract order by employee_id,employee_type_description) = 1)
UNION DISTINCT
SELECT *,
FROM table 1
where work_contract = "CASUAL"
qualify ( row_number() over (partition by work_contract order by employee_id,employee_type_description) = 1)