Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add sql solution to lc problem: No.3156 #2859

Merged
merged 2 commits into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
feat: add sql solution to lc problem: No.3156
No.3156.Employee Task Duration and Concurrent Tasks
  • Loading branch information
yanglbme committed May 20, 2024
commit f1c100ab7b4a46f9c77417f1530884fce40bcc2c
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,41 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3156.Em
#### MySQL

```sql

# Write your MySQL query statement below
WITH
T AS (
SELECT DISTINCT employee_id, start_time AS st
FROM Tasks
UNION DISTINCT
SELECT DISTINCT employee_id, end_time AS st
FROM Tasks
),
P AS (
SELECT
*,
LEAD(st) OVER (
PARTITION BY employee_id
ORDER BY st
) AS ed
FROM T
),
S AS (
SELECT
P.*,
COUNT(1) AS concurrent_count
FROM
P
INNER JOIN Tasks USING (employee_id)
WHERE P.st >= Tasks.start_time AND P.ed <= Tasks.end_time
GROUP BY 1, 2, 3
)
SELECT
employee_id,
FLOOR(SUM(TIME_TO_SEC(TIMEDIFF(ed, st)) / 3600)) AS total_task_hours,
MAX(concurrent_count) AS max_concurrent_tasks
FROM S
GROUP BY 1
ORDER BY 1;
```

<!-- tabs:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,41 @@ Each row in this table contains the task identifier, the employee identifier, an
#### MySQL

```sql

# Write your MySQL query statement below
WITH
T AS (
SELECT DISTINCT employee_id, start_time AS st
FROM Tasks
UNION DISTINCT
SELECT DISTINCT employee_id, end_time AS st
FROM Tasks
),
P AS (
SELECT
*,
LEAD(st) OVER (
PARTITION BY employee_id
ORDER BY st
) AS ed
FROM T
),
S AS (
SELECT
P.*,
COUNT(1) AS concurrent_count
FROM
P
INNER JOIN Tasks USING (employee_id)
WHERE P.st >= Tasks.start_time AND P.ed <= Tasks.end_time
GROUP BY 1, 2, 3
)
SELECT
employee_id,
FLOOR(SUM(TIME_TO_SEC(TIMEDIFF(ed, st)) / 3600)) AS total_task_hours,
MAX(concurrent_count) AS max_concurrent_tasks
FROM S
GROUP BY 1
ORDER BY 1;
```

<!-- tabs:end -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Write your MySQL query statement below
WITH
T AS (
SELECT DISTINCT employee_id, start_time AS st
FROM Tasks
UNION DISTINCT
SELECT DISTINCT employee_id, end_time AS st
FROM Tasks
),
P AS (
SELECT
*,
LEAD(st) OVER (
PARTITION BY employee_id
ORDER BY st
) AS ed
FROM T
),
S AS (
SELECT
P.*,
COUNT(1) AS concurrent_count
FROM
P
INNER JOIN Tasks USING (employee_id)
WHERE P.st >= Tasks.start_time AND P.ed <= Tasks.end_time
GROUP BY 1, 2, 3
)
SELECT
employee_id,
FLOOR(SUM(TIME_TO_SEC(TIMEDIFF(ed, st)) / 3600)) AS total_task_hours,
MAX(concurrent_count) AS max_concurrent_tasks
FROM S
GROUP BY 1
ORDER BY 1;
Loading