-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay_12 - Date Time, Custom Functions.sql
157 lines (138 loc) · 3.5 KB
/
Day_12 - Date Time, Custom Functions.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# Difference between row_number, rank and dense rank
with tie as
(
Select
concat(First,' ',Last) as FullName,
Salary,
Row_number() over (Order by Salary desc) as rw,
Rank() over (Order by Salary desc) as rnk,
Dense_Rank() over (Order by salary desc) as drnk
from employees.employees
Order by salary desc
)
Select *
from tie
where drnk <=3 ;
# Display the list of films in each genre with runtime more than avg runtimeminutes of all films in that genre
with gt as
(
Select
Title,
Genre,
RunTimeMinutes,
Avg(RunTimeMinutes) over (Partition by Genre) as AvgRunTime
from movies.film f inner join movies.Genre g on f.GenreId = g.GenreID
)
Select *
from gt
where RunTimeMinutes > AvgRunTime ;
# Display the list of films in each year with runtime more than avg runtimeminutes of all films in that year
with gt as
(
Select
Year(ReleaseDate) as Year,
RunTimeMinutes,
Avg(RunTimeMinutes) over (Partition by Year(ReleaseDate)) as AvgRunTime
from movies.film
)
Select *
from gt
where RunTimeMinutes > AvgRunTime ;
-- ------------------------------------------------------------------------------------------
# display Year wise % change in hiring
# 2008 100
# 2009 200 100%
# 2010 300 70%
Select
EmployeeId,
concat(First,' ',Last) as FullName,
Salary,
Lead(Salary) over (Order by EmployeeId) as ld
from employees.employees ;
Select
EmployeeId,
concat(First,' ',Last) as FullName,
Salary,
Lag(Salary) over (Order by EmployeeId) as ld
from employees.employees ;
-- ------------------------------------------------------------------------------------------
with ct as
(
Select
Year(Hiredate) as Year,
Count(*) as Number_of_Emp
from employees.employees
Group by Year
)
Select
Year,
Number_of_Emp,
((Number_of_Emp - Lag(Number_of_Emp) over (Order by Year))/Lag(Number_of_Emp) over (Order by Year))*100 as py
from ct ;
# Year, Quarter, % change in hiring compared to prev quarter in each year
with qt as
(
select quarter(Hiredate) as quarter,
year(HireDate) as year,
count(*) as Number_of_Emp
from employees.employees
group by year,quarter
)
select year,quarter,Number_of_Emp,
((number_of_Emp - lag(Number_of_Emp) over (order by Year,quarter))/lag(Number_of_Emp) over (order by year,quarter))*100 as py
from qt
order by year,quarter ;
# Q1 from 2001 with Q1 from 2000
with qt as
(
select quarter(Hiredate) as quarter,
year(HireDate) as year,
count(*) as Number_of_Emp
from employees.employees
group by year,quarter
)
select
year,
quarter,
Number_of_Emp,
lag(Number_of_Emp) over (order by Year) as py
from qt
order by year, quarter ;
# Datetime functions
Select
curdate() as today,
curtime() as now,
now() as dt,
Year(curdate()) as Yr,
Month(curdate()) as Month,
Quarter(curdate()) as Qtr,
MonthName(curdate()) as MN,
DayName(curdate()) as dy,
date_add(curdate(), Interval -3 Year) as da,
datediff('2024-6-28', curdate()) as df,
timestampdiff(Year, curdate(),'2024-6-28') as tm ;
-- ------------------------------------------------------------------------------------------
# Display age of the movie in years and months format
SELECT
Title,
Releasedate,
CONCAT(
TIMESTAMPDIFF(YEAR,Releasedate,CURDATE()),
' Years ',
TIMESTAMPDIFF(MONTH,Releasedate,CURDATE()) % 12,
' Months'
) Age
FROM
movies.film ;
SELECT
Title,
Releasedate,
CONCAT(
TIMESTAMPDIFF(YEAR,Releasedate,CURDATE()),
' Years ',
TIMESTAMPDIFF(MONTH,Releasedate,CURDATE()) % 12,
' Months'
) Age
FROM
movies.film ;
-- ================================================= THE END =================================================