Skip to content

Commit 985dc43

Browse files
committed
task: #1789
1 parent 93fb622 commit 985dc43

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ Have a good contributing!
6666
- [1693. Daily Leads and Partners](./leetcode/easy/1693.%20Daily%20Leads%20and%20Partners.sql)
6767
- [1729. Find Followers Count](./leetcode/easy/1729.%20Find%20Followers%20Count.sql)
6868
- [1741. Find Total Time Spent by Each Employee](./leetcode/easy/1741.%20Find%20Total%20Time%20Spent%20by%20Each%20Employee.sql)
69+
- [1789. Primary Department for Each Employee](./leetcode/easy/1789.%20Primary%20Department%20for%20Each%20Employee.sql)
6970
- [1965. Employees With Missing Information](./leetcode/easy/1965.%20Employees%20With%20Missing%20Information.sql)
7071
- [1978. Employees Whose Manager Left the Company](./leetcode/easy/1978.%20Employees%20Whose%20Manager%20Left%20the%20Company.sql)
7172
- [2356. Number of Unique Subjects Taught by Each Teacher](./leetcode/easy/2356.%20Number%20of%20Unique%20Subjects%20Taught%20by%20Each%20Teacher.sql)
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
Question 1789. Primary Department for Each Employee
3+
Link: https://leetcode.com/problems/primary-department-for-each-employee/description/?envType=study-plan-v2&envId=top-sql-50
4+
5+
Table: Employee
6+
7+
+---------------+---------+
8+
| Column Name | Type |
9+
+---------------+---------+
10+
| employee_id | int |
11+
| department_id | int |
12+
| primary_flag | varchar |
13+
+---------------+---------+
14+
(employee_id, department_id) is the primary key (combination of columns with unique values) for this table.
15+
employee_id is the id of the employee.
16+
department_id is the id of the department to which the employee belongs.
17+
primary_flag is an ENUM (category) of type ('Y', 'N'). If the flag is 'Y', the department is the primary department for the employee. If the flag is 'N', the department is not the primary.
18+
19+
20+
Employees can belong to multiple departments. When the employee joins other departments, they need to decide which department is their primary department. Note that when an employee belongs to only one department, their primary column is 'N'.
21+
22+
Write a solution to report all the employees with their primary department. For employees who belong to one department, report their only department.
23+
24+
Return the result table in any order.
25+
*/
26+
27+
SELECT
28+
e.employee_id,
29+
(CASE
30+
WHEN COUNT(e.employee_id) = 1
31+
THEN (
32+
SELECT e1.department_id
33+
FROM Employee AS e1
34+
WHERE e1.employee_id = e.employee_id
35+
)
36+
ELSE (
37+
SELECT e2.department_id
38+
FROM Employee AS e2
39+
WHERE e2.employee_id = e.employee_id AND e2.primary_flag = 'Y'
40+
)
41+
END) AS department_id
42+
FROM Employee AS e
43+
GROUP BY e.employee_id

0 commit comments

Comments
 (0)