File tree 1 file changed +13
-5
lines changed
src/main/kotlin/g1701_1800/s1789_primary_department_for_each_employee
1 file changed +13
-5
lines changed Original file line number Diff line number Diff line change 1
1
# Write your MySQL query statement below
2
- # #Easy #Database #2023_06_18_Time_1033_ms_(77.87%)_Space_0B_(100.00%)
3
- select e1 .employee_id , case when e2 .department_id is null then e1 .department_id else e2 .department_id end as department_id
4
- from employee e1
5
- left join (select * from employee where primary_flag = ' Y' )e2 on e1 .employee_id = e2 .employee_id
6
- group by e1 .employee_id , department_id
2
+ # #Easy #Database #2023_08_16_Time_1122_ms_(74.47%)_Space_0B_(100.00%)
3
+ WITH cte AS (
4
+ SELECT DISTINCT employee_id, department_id,
5
+ COUNT (employee_id) OVER (PARTITION BY employee_id) AS n
6
+ FROM Employee
7
+ )
8
+ SELECT employee_id, department_id
9
+ FROM cte
10
+ WHERE n = 1
11
+ UNION
12
+ SELECT employee_id, department_id
13
+ FROM Employee
14
+ WHERE primary_flag = ' Y' ;
You can’t perform that action at this time.
0 commit comments