Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 545d0a9

Browse files
Update readme.md
1 parent 03dc4ce commit 545d0a9

File tree

1 file changed

+140
-0
lines changed
  • LeetCode SQL 50 Solution/1789. Primary Department for Each Employee

1 file changed

+140
-0
lines changed
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
Below is a well-structured `README.md` for **LeetCode 1789 - Primary Department for Each Employee**, including both the SQL and Pandas (Python) solutions:
2+
3+
```md
4+
# 🏢 Primary Department for Each Employee - LeetCode 1789
5+
6+
## 📌 Problem Statement
7+
You are given a table **Employee** that contains the following columns:
8+
9+
- **employee_id**: The ID of the employee.
10+
- **department_id**: The ID of the department to which the employee belongs.
11+
- **primary_flag**: An ENUM ('Y', 'N').
12+
- If `primary_flag` is `'Y'`, then the department is the primary department for that employee.
13+
- If `primary_flag` is `'N'`, then the department is not primary.
14+
15+
**Note:**
16+
- An employee can belong to multiple departments. When an employee joins multiple departments, they decide which one is their primary (set to `'Y'`).
17+
- If an employee belongs to only one department, then their `primary_flag` is `'N'`, but that department is still considered their primary department.
18+
19+
Your task is to **report all employees with their primary department**.
20+
For employees who belong to only one department, report that department.
21+
22+
Return the result table in **any order**.
23+
24+
---
25+
26+
## 📊 Table Structure
27+
28+
### **Employee Table**
29+
| Column Name | Type |
30+
| ------------- | ------- |
31+
| employee_id | int |
32+
| department_id | int |
33+
| primary_flag | varchar |
34+
35+
- `(employee_id, department_id)` is the **primary key** for this table.
36+
37+
---
38+
39+
## 📊 Example 1:
40+
41+
### **Input:**
42+
#### **Employee Table**
43+
| employee_id | department_id | primary_flag |
44+
| ----------- | ------------- | ------------ |
45+
| 1 | 1 | N |
46+
| 2 | 1 | Y |
47+
| 2 | 2 | N |
48+
| 3 | 3 | N |
49+
| 4 | 2 | N |
50+
| 4 | 3 | Y |
51+
| 4 | 4 | N |
52+
53+
### **Output:**
54+
| employee_id | department_id |
55+
| ----------- | ------------- |
56+
| 1 | 1 |
57+
| 2 | 1 |
58+
| 3 | 3 |
59+
| 4 | 3 |
60+
61+
### **Explanation:**
62+
- **Employee 1** belongs to only one department (1), so department 1 is their primary.
63+
- **Employee 2** belongs to departments 1 and 2. The row with `primary_flag = 'Y'` indicates that department 1 is their primary.
64+
- **Employee 3** belongs only to department 3.
65+
- **Employee 4** belongs to departments 2, 3, and 4. The row with `primary_flag = 'Y'` indicates that department 3 is their primary.
66+
67+
---
68+
69+
## 🖥 SQL Solution
70+
71+
### ✅ **Approach:**
72+
- **Step 1:** For employees who have `primary_flag = 'Y'`, choose those rows.
73+
- **Step 2:** For employees who belong to only one department, return that row.
74+
- Combine the results using `UNION DISTINCT`.
75+
76+
```sql
77+
SELECT employee_id, department_id
78+
FROM Employee
79+
WHERE primary_flag = 'Y'
80+
UNION DISTINCT
81+
SELECT employee_id, department_id
82+
FROM Employee
83+
GROUP BY employee_id
84+
HAVING COUNT(*) = 1;
85+
```
86+
87+
---
88+
89+
## 🐍 Python (Pandas) Solution
90+
91+
### **Approach:**
92+
1. **Group** the DataFrame by `employee_id`.
93+
2. For each group:
94+
- If any row has `primary_flag == 'Y'`, choose the first such row.
95+
- Otherwise (i.e., employee belongs to only one department), choose that row.
96+
3. Return the resulting DataFrame with only `employee_id` and `department_id`.
97+
98+
```python
99+
import pandas as pd
100+
101+
def primary_department(employees: pd.DataFrame) -> pd.DataFrame:
102+
def select_primary(group):
103+
# If there's any row with primary_flag 'Y', choose the first one
104+
if (group['primary_flag'] == 'Y').any():
105+
return group[group['primary_flag'] == 'Y'].iloc[0]
106+
else:
107+
# For employees with only one department
108+
return group.iloc[0]
109+
110+
result = employees.groupby('employee_id').apply(select_primary).reset_index(drop=True)
111+
return result[['employee_id', 'department_id']]
112+
```
113+
114+
---
115+
116+
## 📁 File Structure
117+
```
118+
📂 Primary-Department
119+
│── README.md
120+
│── solution.sql
121+
│── solution_pandas.py
122+
│── test_cases.sql
123+
│── sample_data.csv
124+
```
125+
126+
---
127+
128+
## 🔗 Useful Links
129+
- 📖 [LeetCode Problem](https://leetcode.com/problems/primary-department-for-each-employee/)
130+
- 🔍 [MySQL UNION Operator](https://www.w3schools.com/sql/sql_union.asp)
131+
- 🐍 [Pandas GroupBy Documentation](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.groupby.html)
132+
```
133+
134+
This `README.md` includes:
135+
- A **clear problem statement** with table structure and example.
136+
- A detailed **SQL solution** with an explanation.
137+
- A detailed **Pandas (Python) solution** with an explanation.
138+
- An organized **file structure** and helpful external links.
139+
140+
Let me know if you need any modifications!

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /