Skip to content

Commit 75d8fb3

Browse files
committedJun 24, 2024
new code
1 parent 0a53bfb commit 75d8fb3

File tree

2 files changed

+94
-0
lines changed

2 files changed

+94
-0
lines changed
 

‎30_question/category_month_wise.sql

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
SELECT
2+
category,
3+
SUM(CASE WHEN Extract(month from transaction_date) = 1 THEN amount ELSE 0 END) AS January,
4+
SUM(CASE WHEN Extract(month from transaction_date) = 2 THEN amount ELSE 0 END) AS Feburary,
5+
SUM(CASE WHEN Extract(month from transaction_date) = 3 THEN amount ELSE 0 END) AS March,
6+
SUM(CASE WHEN Extract(month from transaction_date) = 4 THEN amount ELSE 0 END) AS April,
7+
SUM(CASE WHEN Extract(month from transaction_date) = 5 THEN amount ELSE 0 END) AS May,
8+
SUM(CASE WHEN Extract(month from transaction_date) = 6 THEN amount ELSE 0 END) AS June,
9+
SUM(CASE WHEN Extract(month from transaction_date) = 7 THEN amount ELSE 0 END) AS July,
10+
SUM(CASE WHEN Extract(month from transaction_date) = 8 THEN amount ELSE 0 END) AS August,
11+
SUM(CASE WHEN Extract(month from transaction_date) = 9 THEN amount ELSE 0 END) AS September,
12+
SUM(CASE WHEN Extract(month from transaction_date) = 10 THEN amount ELSE 0 END) AS October,
13+
SUM(CASE WHEN Extract(month from transaction_date) = 11 THEN amount ELSE 0 END) AS November,
14+
SUM(CASE WHEN Extract(month from transaction_date) = 12 THEN amount ELSE 0 END) AS December
15+
FROM
16+
transactions
17+
GROUP BY
18+
category
19+
ORDER BY
20+
category;
21+

‎tricky_questions/attende_interval.sql

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
2+
/*
3+
4+
-- Create the EmployeeAttendance table
5+
CREATE TABLE EmployeeAttendance (
6+
EmployeeID VARCHAR(10),
7+
Date DATE,
8+
Status VARCHAR(10)
9+
);
10+
11+
-- Input
12+
13+
INSERT INTO EmployeeAttendance (EmployeeID, Date, Status) VALUES
14+
('A1', '2024-01-01', 'PRESENT'),
15+
('A1', '2024-01-02', 'PRESENT'),
16+
('A1', '2024-01-03', 'PRESENT'),
17+
('A1', '2024-01-04', 'ABSENT'),
18+
('A1', '2024-01-05', 'PRESENT'),
19+
('A1', '2024-01-06', 'PRESENT'),
20+
('A1', '2024-01-07', 'ABSENT'),
21+
('A1', '2024-01-08', 'ABSENT'),
22+
('A1', '2024-01-09', 'ABSENT'),
23+
('A1', '2024-01-10', 'PRESENT'),
24+
('A2', '2024-01-06', 'PRESENT'),
25+
('A2', '2024-01-07', 'PRESENT'),
26+
('A2', '2024-01-08', 'ABSENT'),
27+
('A2', '2024-01-09', 'PRESENT'),
28+
('A2', '2024-01-10', 'ABSENT');
29+
30+
31+
-- Output
32+
33+
EMPLOYEE | FROM_DATE | TO_DATE | STATUS
34+
--------------------------------------------
35+
A1 | 2024-01-01 | 2024-01-03 | PRESENT
36+
A1 | 2024-01-04 | 2024-01-04 | ABSENT
37+
A1 | 2024-01-05 | 2024-01-06 | PRESENT
38+
A1 | 2024-01-07 | 2024-01-09 | ABSENT
39+
A1 | 2024-01-10 | 2024-01-10 | PRESENT
40+
A2 | 2024-01-06 | 2024-01-07 | PRESENT
41+
A2 | 2024-01-08 | 2024-01-08 | ABSENT
42+
A2 | 2024-01-09 | 2024-01-09 | PRESENT
43+
A2 | 2024-01-10 | 2024-01-10 | ABSENT
44+
45+
*/
46+
47+
with cte as (select
48+
EmployeeID,
49+
Date,
50+
Status,
51+
date - CAST(ROW_NUMBER() OVER (ORDER BY date) AS INT) AS diff
52+
from EmployeeAttendance)
53+
select
54+
EmployeeID,
55+
Status,
56+
diff,
57+
date,
58+
row_number() over(partition by EmployeeID, Status order by date)
59+
from cte
60+
-- group by EmployeeID, Status, diff
61+
-- order by 3 desc
62+
63+
64+
65+
-- SELECT
66+
-- EmployeeID,
67+
-- STRING_AGG(Date::text, ', ') AS Dates,
68+
-- Status
69+
-- FROM
70+
-- EmployeeAttendance
71+
-- GROUP BY
72+
-- EmployeeID,
73+
-- Status;

0 commit comments

Comments
 (0)
Please sign in to comment.