-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSQL.sql
170 lines (137 loc) · 4.15 KB
/
SQL.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
CREATE DATABASE AssignmentDWH;
USE AssignmentDWH;
GO
-- Create the source table
CREATE TABLE Employee_Q3 (
ID INT,
Name VARCHAR(255),
City VARCHAR(255),
Email VARCHAR(255),
Schedule_Date DATE
);
-- Populate the source table with sample data
INSERT INTO Employee_Q3 (ID, Name, City, Email, Schedule_Date)
VALUES
(1001, 'Ahmed', 'zayed1ppppp', 'ahmed@mail.com', GETDATE()+1),
(1002, 'Mohamed', 'zayed2ddd', 'nehal@mail.com', GETDATE()+1),
(1003, 'zeyad', 'zayed3mmmmm', 'asem@mail.com', GETDATE()+1);
SELECT * FROM Employee_Q3;
--TRUNCATE TABLE Employee_Q3;
-- Create the target table if it doesn't exist
CREATE TABLE Target_Table (
Emp_Key INT IDENTITY(1,1) PRIMARY KEY,
ID INT,
Name VARCHAR(255),
City VARCHAR(255),
Email VARCHAR(255),
Insert_Date DATE,
Active_Flag INT,
Version_No INT
);
SELECT* FROM Target_Table;
--TRUNCATE TABLE Target_Table;
-----------------------------Q2
--DROP TABLE Employee_Q2
CREATE TABLE Employee_Q2 (
ID INT,
Name VARCHAR(50),
City VARCHAR(50),
Email VARCHAR(100),
Update_Date datetime,
);
INSERT INTO Employee_Q2 (ID, Name, City, Email, Update_Date)
VALUES
(1001, 'Ahmed', 'Cairo', 'ahmed@mail.com', '2024-04-20'),
(1002, 'Nehal', 'Giza', 'nehal@mail.com', '2024-04-20'),
(1003, 'Asem', 'Cairo', 'asem@mail.com', '2024-04-20');
-- Create the target table
CREATE TABLE Employee_Latest (
ID INT PRIMARY KEY,
Name VARCHAR(255),
City VARCHAR(255),
Email VARCHAR(255),
Update_Date datetime,
);
-- Insert changes into the history table
Drop table Employee_History
CREATE TABLE Employee_History (
History_ID INT IDENTITY(1,1) PRIMARY KEY,
ID INT,
Name VARCHAR(255),
City VARCHAR(255),
Email VARCHAR(255),
Update_Date DATEtime,
End_Date DATE
);
CREATE TABLE Config_table(
table_name VARCHAR(255),
last_extract_date datetime
);
INSERT INTO Config_table (table_name,last_extract_date)
VALUES ('Employee_Q2','1950-1-1');
SELECT * FROM Employee_Q2 ;
SELECT * FROM Employee_Latest ;
SELECT * FROM Employee_History ;
SELECT * FROM Config_table;
TRUNCATE TABLE Employee_Q2;
TRUNCATE TABLE Employee_Latest;
TRUNCATE TABLE Employee_History;
TRUNCATE TABLE Config_table;
--------------------Q4
CREATE TABLE Attendance_Device
(
id smallint PRIMARY KEY,
employee_id smallint,
finger_print_ts datetime,
in_out varchar(3)
);
INSERT INTO Attendance_Device (id, employee_id, finger_print_ts, in_out) VALUES
('1', '101', '2024-03-12 9:00:00', 'in'),
('2', '101', '2024-03-12 10:00:00', 'in'),
('3', '102', '2024-03-12 9:00:00', 'in'),
('4', '103', '2024-03-12 11:00:00', 'in'),
('5', '104', '2024-03-12 9:15:00', 'in'),
('6', '105', '2024-03-12 10:00:00', 'in'),
('7', '105', '2024-03-12 11:00:00', 'in'),
('8', '105', '2024-03-12 11:30:00', 'in'),
('9', '106', '2024-03-12 9:00:00', 'in'),
('10', '107', '2024-03-12 9:00:00', 'in'),
('11', '108', '2024-03-12 9:00:00', 'in'),
('12', '101', '2024-03-12 9:00:00', 'out'),
('13', '101', '2024-03-12 17:00:00', 'out'),
('14', '101', '2024-03-12 19:00:00', 'out'),
('15', '102', '2024-03-12 17:00:00', 'out'),
('16', '103', '2024-03-12 17:00:00', 'out'),
('17', '105', '2024-03-12 10:00:00', 'out'),
('18', '105', '2024-03-12 11:00:00', 'out'),
('19', '105', '2024-03-12 18:00:00', 'out'),
('20', '106', '2024-03-12 19:00:00', 'out'),
('21', '107', '2024-03-12 14:00:00', 'out'),
('22', '108', '2024-03-12 17:00:00', 'out');
SELECT * FROM Attendance_Device;
--TERUNCATE TABLE Attendance_Device;
Drop Table Employee_Attendance_Details
CREATE TABLE Employee_Attendance_Details (
Att_Key smallint IDENTITY(1,1) PRIMARY KEY,
Emp_ID smallint,
Date DATE,
Time_In NVARCHAR(5),
Time_Out NVARCHAR(5),
Worked_Hours smallint,
State NVARCHAR(50)
);
SELECT * FROM Attendance_Device;
SELECT * FROM Employee_Attendance_Details;
TRUNCATE TABLE Employee_Attendance_Details;
TRUNCATE TABLE Attendance_Device;
CREATE TABLE University (
Name NVARCHAR(MAX),
StateProvince NVARCHAR(MAX),
Domains NVARCHAR(MAX),
WebPages NVARCHAR(MAX),
Country NVARCHAR(MAX),
AlphaTwoCode NVARCHAR(2)
);
SELECT * FROM University
order by Name;
TRUNCATE TABLE University;