-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWinterProject.sql
229 lines (175 loc) · 6.17 KB
/
WinterProject.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
--Creating Database
CREATE DATABASE WinterProject
GO
USE WinterProject
GO
--Creating Tables
CREATE TABLE dbo.SeniorityLevel(
[Id] INT IDENTITY (1,1) NOT NULL,
[Name] NVARCHAR (100) NOT NULL,
CONSTRAINT PK_SeniorityLevel PRIMARY KEY CLUSTERED ([Id] ASC)
)
GO
CREATE TABLE dbo.[Location](
[Id] INT IDENTITY(1,1) NOT NULL,
[CountryName] NVARCHAR (100) NULL,
[Continent] NVARCHAR (100) NULL,
[Region] NVARCHAR (100) NULL,
CONSTRAINT PK_Location PRIMARY KEY CLUSTERED ([Id] ASC)
)
GO
CREATE TABLE dbo.Department(
[Id] INT IDENTITY (1,1) NOT NULL,
[Name] NVARCHAR (100) NOT NULL,
CONSTRAINT PK_Department PRIMARY KEY CLUSTERED ([Id] ASC)
)
GO
CREATE TABLE dbo.Employee(
[Id] INT IDENTITY (1,1) NOT NULL,
[FirstName] NVARCHAR (100) NOT NULL,
[LastName] NVARCHAR(100) NOT NULL,
[LocationId] INT NOT NULL,
[SeniorityLevelId] INT NOT NULL,
[DepartmentId] INT NOT NULL,
CONSTRAINT PK_Employee PRIMARY KEY CLUSTERED ([Id] ASC)
)
GO
CREATE TABLE dbo.Salary(
[Id] BIGINT IDENTITY (1,1) NOT NULL,
[EmployeeId] INT NOT NULL,
[Month] SMALLINT NOT NULL,
[Year] SMALLINT NOT NULL,
[GrossAmount] DECIMAL (18,2) NOT NULL,
[NetAmount] DECIMAL(18,2) NOT NULL,
[RegularWorkAmount] DECIMAL (18,2) NOT NULL,
[BonusAmount] DECIMAL (18,2) NOT NULL,
[OvertimeAmount] DECIMAL (18,2) NOT NULL,
[VacationDays] SMALLINT NOT NULL,
[SickLeaveDays] SMALLINT NOT NULL,
CONSTRAINT PK_Salary PRIMARY KEY CLUSTERED ([Id] ASC)
)
GO
--Adding foregin keys
ALTER TABLE dbo.Employee WITH CHECK
ADD CONSTRAINT FK_Employee_SeniorityLevel FOREIGN KEY (SeniorityLevelId)
REFERENCES dbo.SeniorityLevel (Id)
ALTER TABLE dbo.Employee WITH CHECK
ADD CONSTRAINT FK_Employee_Location FOREIGN KEY (LocationId)
REFERENCES dbo.Location (Id)
ALTER TABLE dbo.Employee WITH CHECK
ADD CONSTRAINT FK_Employee_Department FOREIGN KEY (DepartmentId)
REFERENCES dbo.Department (Id)
ALTER TABLE dbo.Salary WITH CHECK
ADD CONSTRAINT FK_Salary_Employee FOREIGN KEY (EmployeeId)
REFERENCES dbo.Employee (Id)
--Populating tables
--SeniorityLevel
INSERT INTO dbo.SeniorityLevel ([Name])
VALUES ('Junior'),('Intermediate'),('Senor'),('Lead'),('Project Manager'),
('Division Manager'),('Office Manager'),('CEO'),('CTO'),('CIO')
GO
SELECT * FROM dbo.SeniorityLevel
--Location
GO
CREATE OR ALTER PROCEDURE dbo.InsertLocation
AS
BEGIN
INSERT INTO dbo.Location (CountryName,Continent,Region)
SELECT AC.CountryName,AC.Continent,AC.Region
FROM WideWorldImporters.Application.Countries AS AC
END
GO
EXEC dbo.InsertLocation
SELECT * FROM dbo.Location
--Department
INSERT INTO dbo.Department ([Name])
VALUES ('Personal Banking & Operations'),('Digital Banking Department'),('Retail Banking & Marketing Department'),
('Wealth Managment & Third Party Products'),('International Banking Division & DFB'),('Treasury'),
('Information Technology'),('Corporate Communications'),('Support Services & Branch Expansion'),
('Human Resources')
GO
SELECT * FROM dbo.Department
--Employee
GO
CREATE OR ALTER PROCEDURE dbo.InsertEmployee
AS
BEGIN
;WITH CTE AS(
SELECT P.PersonID AS ID,
LEFT(P.FullName, CHARINDEX(' ',P.FullName) - 1) AS FName,
SUBSTRING(P.FullName, CHARINDEX(' ', P.FullName) + 1, LEN(P.FullName)) AS LName
FROM WideWorldImporters.Application.People AS P)
INSERT INTO dbo.Employee ( FirstName, LastName, LocationId, SeniorityLevelId, DepartmentId)
SELECT CTE.FName AS FirstName, CTE.LName AS LastName,
NTILE (190) OVER (ORDER BY L.ID) AS LocationID,
NTILE (10) OVER (ORDER BY S.ID) AS SeniorityLevelId,
NTILE (10) OVER (ORDER BY D.ID) AS DepartmentId
FROM CTE
LEFT OUTER JOIN dbo.Employee AS E ON E.Id = CTE.ID
LEFT OUTER JOIN dbo.Location AS L ON L.Id = E.LocationId
LEFT OUTER JOIN dbo.SeniorityLevel AS S ON E.SeniorityLevelId = S.Id
LEFT OUTER JOIN dbo.Department AS D ON E.DepartmentId = D.Id
END
GO
EXEC dbo.InsertEmployee
SELECT * FROM dbo.Employee
--Salary
GO
CREATE OR ALTER PROCEDURE dbo.InsertSalary
AS
BEGIN
CREATE TABLE #Dates([Year] SMALLINT, [Month] SMALLINT)
DECLARE @StartYear SMALLINT = 2001; DECLARE @EndYear SMALLINT = 2020;
WHILE @StartYear <= @EndYear
BEGIN
DECLARE @StartMonth SMALLINT = 1; DECLARE @EndMonth SMALLINT = 12;
WHILE @StartMonth <= @EndMonth
BEGIN
INSERT INTO #Dates([Year],[Month])
SELECT @StartYear, @StartMonth
SET @StartMonth += 1
END
SET @StartYear += 1
END
;WITH CTEA AS(
SELECT E.Id,D.[Month] AS [Month],D.[Year] AS [Year], (ABS(CHECKSUM(NEWID()))%30000+ 30000 + 1) AS GrossAmount
FROM #Dates AS D
CROSS JOIN Employee AS E),
CTEB AS(
SELECT*, C.GrossAmount * 0.9 AS NetAmount, (c.GrossAmount*0.9)*0.8 AS RegularWorkAmount
FROM CTEA AS C)
INSERT INTO dbo.Salary([EmployeeId], [Month], [Year], [GrossAmount], [NetAmount], [RegularWorkAmount],
[BonusAmount], [OvertimeAmount], [VacationDays], [SickLeaveDays])
SELECT B.Id AS EmpolyeeId, B.Month, B.Year, B.GrossAmount,B.NetAmount,B.RegularWorkAmount,
CASE
WHEN (B.Month%2)=1 THEN B.NetAmount - B.RegularWorkAmount
ELSE 0 END AS BonusAmount,
CASE
WHEN (B.Month%2)=0 THEN B.NetAmount - B.RegularWorkAmount
ELSE 0 END AS OvertimeAmount,
CASE
WHEN (B.Month in (7,12)) THEN 10
ELSE 0 END AS VacationDays,
0 AS SickLeaveDays
FROM CTEB AS B
END
GO
EXEC dbo.InsertSalary
UPDATE dbo.Salary
SET VacationDays = VacationDays + (EmployeeId % 2)
WHERE (EmployeeId + [Month] + [Year])%5 = 1
GO
UPDATE dbo.Salary
SET SickLeaveDays = EmployeeId%8, VacationDays = VacationDays + (EmployeeId % 3)
WHERE (EmployeeId + [Month] + [Year] )%5 = 2
GO
SELECT * FROM Salary
--Checking if the query returns 0 rows
SELECT * FROM Salary
WHERE NetAmount <> (regularWorkAmount + BonusAmount + OverTimeAmount)
--Checking if the sum of VacationDays is between 20 and 30 days
SELECT Employeeid, [Year], SUM(VacationDays) FROM dbo.SalaryGROUP BY EmployeeId,[Year]HAVING SUM(VacationDays) BETWEEN 20 AND 30ORDER BY EmployeeId,[Year]