Skip to content

Commit 4a03ee6

Browse files
Added Clocked Hours
1 parent 02fec56 commit 4a03ee6

File tree

3 files changed

+135
-0
lines changed

3 files changed

+135
-0
lines changed

Clocked Hours/READme.md

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# Clocked Hours
2+
![Star Badge](https://img.shields.io/static/v1?label=%F0%9F%8C%9F&message=If%20Useful&style=style=flat&color=BC4E99)
3+
[![View Main Folder](https://img.shields.io/badge/View-Main_Folder-971901?)](https://github.com/thecoddiwompler/SQL-Practice-Questions/tree/main)
4+
[![View Repositories](https://img.shields.io/badge/View-My_Repositories-blue?logo=GitHub)](https://github.com/thecoddiwompler?tab=repositories)
5+
[![View My Profile](https://img.shields.io/badge/View-My_Profile-green?logo=GitHub)](https://github.com/thecoddiwompler)
6+
7+
---
8+
9+
## 🛠️ Problem Statement
10+
11+
12+
<b>Table Name : clocked_hours</b>
13+
</br>
14+
| Column Name |Type |
15+
| ------------- | ------------- |
16+
| empd_id | int |
17+
| swipe | time |
18+
| flag | char |
19+
20+
21+
* <b> (empd_id, swipe) is the primary key for this table. </b><br/>
22+
<br/>
23+
24+
This table provides empd_id (Employee ID), swipe time (swipe), and swipe type (flag). Flag can have only two values- 'I' and 'O' signifying Entry time and exit time.
25+
<br/>
26+
</br>
27+
<b>Write a SQL query to find the total clocked time for each employees.</b>
28+
</br>
29+
<br/>
30+
Return the result table in any order.
31+
</br>
32+
</br>
33+
<b>The query result format is in the following example: </b>
34+
</br>
35+
</br>
36+
37+
<details>
38+
<summary>
39+
Input
40+
</summary>
41+
<br>
42+
<b>Table Name : clocked_hours</b>
43+
<br><br>
44+
45+
| empd_id | swipe | flag |
46+
|---------|--------|------|
47+
| 11114 | 08:30 | I |
48+
| 11114 | 10:30 | O |
49+
| 11114 | 11:30 | I |
50+
| 11114 | 15:30 | O |
51+
| 11115 | 09:30 | I |
52+
| 11115 | 17:30 | O |
53+
54+
55+
56+
</details>
57+
58+
<details>
59+
<summary>
60+
Output
61+
</summary>
62+
<br>
63+
64+
| empd_id | clocked_time |
65+
| ---- |----|
66+
| 11114 | 6 Hours 0 Minutes 0.00 Seconds |
67+
| 11115 | 8 Hours 0 Minutes 0.00 Seconds |
68+
</details>
69+
70+
---

Clocked Hours/schema.sql

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
CREATE TABLE clocked_hours(empd_id int, swipe time, flag char);
2+
3+
INSERT INTO
4+
clocked_hours
5+
VALUES
6+
(11114, '08:30', 'I'),
7+
(11114, '10:30', 'O'),
8+
(11114, '11:30', 'I'),
9+
(11114, '15:30', 'O'),
10+
(11115, '09:30', 'I'),
11+
(11115, '17:30', 'O');
12+
13+
COMMIT;

Clocked Hours/solution.sql

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
WITH cte AS (
2+
SELECT
3+
a.empd_id,
4+
a.swipe entry,
5+
b.swipe exit
6+
FROM
7+
clocked_hours a
8+
INNER JOIN clocked_hours b ON a.empd_id = b.empd_id
9+
AND a.swipe <= b.swipe
10+
AND a.flag = 'I'
11+
AND b.flag = 'O'
12+
),
13+
swipe_details AS (
14+
SELECT
15+
empd_id,
16+
entry,
17+
min(exit) exit
18+
FROM
19+
cte
20+
GROUP BY
21+
empd_id,
22+
entry
23+
)
24+
SELECT
25+
empd_id,
26+
CONCAT(
27+
extract(
28+
HOUR
29+
FROM
30+
SUM(exit - entry)
31+
),
32+
' Hours ',
33+
extract(
34+
MINUTE
35+
FROM
36+
SUM(exit - entry)
37+
),
38+
' Minutes ',
39+
ROUND(
40+
extract(
41+
SECOND
42+
FROM
43+
SUM(exit - entry)
44+
),
45+
2
46+
),
47+
' Seconds'
48+
) clocked_time
49+
FROM
50+
swipe_details
51+
GROUP BY
52+
empd_id;

0 commit comments

Comments
 (0)