-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathl8.sql
More file actions
963 lines (766 loc) · 31.4 KB
/
l8.sql
File metadata and controls
963 lines (766 loc) · 31.4 KB
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
/*=====================================================================*/
/* Lab 8 – Database and Warehouse */
/* Author- Ronak Patel */
/*=====================================================================*/
/*=====================================================================*/
/* Section 1: Lab 5 Create WonderfulWheels databse and requiredtables */
/*=====================================================================*/
USE master; --Use Master Database
GO
--Create WonderfulWheels database, if exist, drop existing databse before creating a new one.
IF DB_ID (N'WonderfulWheels') IS NOT NULL
DROP DATABASE WonderfulWheels;
GO
CREATE DATABASE WonderfulWheels
GO
--UseWonderfulWheels Database from now on.
USE WonderfulWheels;
GO
--Start creating the table
/*==============================================================*/
/* Table: Location */
/*==============================================================*/
CREATE TABLE [Location] (
LocationID int identity(1,1) NOT NULL,
StreeAddress nvarchar(100) NOT NULL,
City nvarchar(50) NOT NULL,
Province char(2) NOT NULL,
PostalCode char(7) NOT NULL,
CONSTRAINT PK_LOCATION PRIMARY KEY (LocationID)
)
GO
/*==============================================================*/
/* Table: Dealership */
/*==============================================================*/
CREATE TABLE Dealership (
DealershipID int IDENTITY(1,1) NOT NULL,
LocationID int NOT NULL,
DealerName nvarchar(50) NOT NULL,
Phone nvarchar(20) NULL,
CONSTRAINT PK_DEALERSHIP PRIMARY KEY (DealershipID),
CONSTRAINT FK_DEAL_LOC FOREIGN KEY (LocationID) REFERENCES Location (LocationID)
)
GO
/*==============================================================*/
/* Table: Person */
/*==============================================================*/
CREATE TABLE Person (
PersonID int IDENTITY(1000,1) NOT NULL,
FirstName nvarchar(50) NOT NULL,
LastName nvarchar(50) NOT NULL,
Phone nvarchar(20) NULL,
Email nvarchar(100) NULL,
PerLocID int NOT NULL,
DateofBirth date NULL,
Title char(2) NULL,
CONSTRAINT PK_PERSON PRIMARY KEY (PersonID),
CONSTRAINT FK_PER_LOC FOREIGN KEY (PerLocID) REFERENCES Location (LocationID),
CONSTRAINT CHK_TITLE CHECK (Title='Mr' OR Title ='Ms')
)
GO
/*==============================================================*/
/* Index: IndexPersonName */
/*==============================================================*/
CREATE INDEX IndexPersonName ON Person ( FirstName ASC, LastName ASC)
GO
/*==============================================================*/
/* Table: Customer */
/*==============================================================*/
CREATE TABLE Customer (
CustomerID int NOT NULL,
RegDate date NOT NULL,
CONSTRAINT PK_CUSTOMER PRIMARY KEY (CustomerID),
CONSTRAINT FK_CUS_PER FOREIGN KEY (CustomerID) REFERENCES Person (PersonID)
)
GO
/*==============================================================*/
/* Table: Employee */
/*==============================================================*/
CREATE TABLE Employee (
EmployeeID int NOT NULL,
EmpDealID int NOT NULL,
HireDate date NOT NULL,
EmpRole nvarchar(50) NOT NULL,
ManagerID int NULL,
CONSTRAINT PK_EMPLOYEE PRIMARY KEY (EmployeeID),
CONSTRAINT FK_EMP_PER FOREIGN KEY (EmployeeID) REFERENCES Person (PersonID),
CONSTRAINT FK_EMP_DEAL FOREIGN KEY (EmpDealID) REFERENCES Dealership (DealershipID),
CONSTRAINT FK_PER_MAN FOREIGN KEY (ManagerID) REFERENCES Employee (EmployeeID)
)
GO
/*==============================================================*/
/* Table: SalaryEmployee */
/* Set Salary to Default 1000 since Check contraint should */
/* not be less than 1000 */
/*==============================================================*/
CREATE TABLE SalaryEmployee (
EmployeeID int NOT NULL,
Salary decimal(12,2) NOT NULL DEFAULT 1000.00,
CONSTRAINT PK_SALEMPLOYEE PRIMARY KEY (EmployeeID),
CONSTRAINT FK_SEMP_EMP FOREIGN KEY (EmployeeID) REFERENCES Employee (EmployeeID),
CONSTRAINT CHK_SALARY CHECK (Salary>=1000)
)
GO
/*==============================================================*/
/* Table: CommissionEmployee */
/* Set Commission to Default 10 since Check contraint should */
/* not be less than 10 */
/*==============================================================*/
CREATE TABLE CommissionEmployee (
EmployeeID int NOT NULL,
Commission decimal(12,2) NOT NULL DEFAULT 10.00,
CONSTRAINT PK_COMEMPLOYEE PRIMARY KEY (EmployeeID),
CONSTRAINT FK_CEMP_EMP FOREIGN KEY (EmployeeID) REFERENCES Employee (EmployeeID),
CONSTRAINT CHK_COMMISSION CHECK (Commission>=10)
)
GO
/*==============================================================*/
/* Table: Vehicle */
/* Set Price to Default 1 since Check contraint should */
/* not be less than 1 */
/* Set VehicleYear Check contraint to be greater than 1800 */
/* and less than current year to capture appropriate Year */
/*==============================================================*/
CREATE TABLE Vehicle (
VehicleID int IDENTITY(100001,1) NOT NULL,
Make nvarchar(50) NOT NULL,
Model nvarchar(50) NOT NULL,
VehicleYear int NOT NULL,
Colour nvarchar(10) NOT NULL,
KM int NOT NULL,
Price decimal(12,2) NULL DEFAULT 1.00,
CONSTRAINT PK_VEHICLE PRIMARY KEY (VehicleID),
CONSTRAINT CHK_PRICE CHECK (Price>=1),
CONSTRAINT CHK_YEAR CHECK (VehicleYear>=1800 AND VehicleYear <= YEAR(GETDATE()))
)
GO
/*==============================================================*/
/* Table: VehicleOrder */
/*==============================================================*/
CREATE TABLE [Order] (
OrderID int IDENTITY(1,1) NOT NULL,
OrderCustID int NOT NULL,
OrderEmpID int NOT NULL,
OrderDate date NOT NULL DEFAULT GetDate(),
OrderDealID int NOT NULL,
CONSTRAINT PK_ORDER PRIMARY KEY (OrderID),
CONSTRAINT FK_ORD_CUST FOREIGN KEY (OrderCustID) REFERENCES Customer (CustomerID),
CONSTRAINT FK_ORD_EMP FOREIGN KEY (OrderEmpID) REFERENCES Employee (EmployeeID),
CONSTRAINT FK_ORD_DEAL FOREIGN KEY (OrderDealID) REFERENCES Dealership (DealershipID)
)
GO
/*==============================================================*/
/* Table: VOrderItem */
/* Set FinalSalePrice to Default 1 since Check contraint should */
/* not be less than 1 */
/*==============================================================*/
CREATE TABLE OrderItem (
OrderID int NOT NULL,
VehicleID int NOT NULL,
FinalSalePrice decimal(12,2) NULL DEFAULT 1.00,
CONSTRAINT PK_ORDERITEM PRIMARY KEY (OrderID, VehicleID),
CONSTRAINT FK_ORDITM_ORD FOREIGN KEY (OrderID) REFERENCES [Order] (OrderID),
CONSTRAINT FK_ORDITM_VEHICLE FOREIGN KEY (VehicleID) REFERENCES Vehicle (VehicleID),
CONSTRAINT CHK_FINALSALEPRICE CHECK (FinalSalePrice>=1)
)
GO
/*==============================================================*/
/* Table: Account */
/*==============================================================*/
CREATE TABLE Account (
AccountID int IDENTITY(1,1) NOT NULL,
CustomerID int NOT NULL,
AccountBalance decimal(12,2) NOT NULL DEFAULT 0.00,
LastPaymentAmount decimal(12,2) NOT NULL DEFAULT 0.00,
LastPaymentDate date NULL,
CONSTRAINT PK_ACCOUNT PRIMARY KEY (AccountID),
CONSTRAINT FK_ACC_CUST FOREIGN KEY (CustomerID) REFERENCES Customer (CustomerID),
CONSTRAINT CHK_BALANCE CHECK (AccountBalance>=0),
CONSTRAINT CHK_AMOUNT CHECK (LastPaymentAmount>=0)
)
GO
/*=====================================================================*/
/* Section 2: Lab 5 Insert new records into the database tables */
/*=====================================================================*/
----------------------------------------------------------------------------------------------------
----------------------- Transaction to insert new data into the database ---------------------------
BEGIN TRY
BEGIN TRANSACTION
---------------Insert all locations into the Location Table---------------
INSERT INTO Location ([StreeAddress],[City],[Province],[PostalCode])
VALUES
('22 Queen St', 'Waterloo', 'ON', 'N2A48B'),
('44 King St', 'Guelph', 'ON', 'G2A47U'),
('55 Krug St', 'Kitchener', 'ON', 'N2A4U7'),
('77 Lynn Ct', 'Toronto', 'ON', 'M7U4BA'),
('88 King St', 'Guelph', 'ON', 'G2A47U'),
('99 Lynn Ct', 'Toronto', 'ON', 'M7U4BA'),
('44 Cedar St', 'Kitchener', 'ON', 'N2A7L6'),
('221 Kitng St W', 'Kitchner', 'ON', 'G8B3C6'),
('77 Victoria St N', 'Campbridge', 'ON', 'N1Z8B8'),
('100 White Oak Rd', 'London', 'ON', 'L9B1W2')
--------------------Insert vehicles into the Vehicle Table-------------------------
INSERT INTO [dbo].[Vehicle]([Make],[Model],[VehicleYear],[Colour],[KM],[Price])
VALUES
('Toyota', 'Corola', '2018', 'Silver', '45000', '18000'),
('Toyota', 'Corola', '2016', 'White', '60000', '15000'),
('Toyota', 'Corola', '2016', 'Black', '65000', '14000'),
('Toyota', 'Camry', '2018', 'White', '35000', '22000'),
('Honda', 'Acord', '2020', 'Gray', '10000', '24000'),
('Honda', 'Acord', '2015', 'Red', '85000', '16000'),
('Honda', 'Acord', '2000', 'Gray', '10000', '40000'),
('Ford', 'Focus', '2017', 'Blue', '40000', '16000')
---------------Insert Kitchener Dealership into the Dealership Table---------------
DECLARE @Street INT
DECLARE @PerID INT
DECLARE @DealID INT
SELECT @Street = LocationID
FROM [Location]
WHERE [Location].StreeAddress ='221 Kitng St W'
INSERT INTO Dealership ([LocationID],[DealerName],[Phone])
VALUES(@Street, 'Kitchener', '519-222-2222')
SELECT @Street = LocationID
FROM [Location]
WHERE [Location].StreeAddress ='77 Victoria St N'
INSERT INTO Dealership ([LocationID],[DealerName],[Phone])
VALUES(@Street, 'Cambrige', '519-222-5555')
SELECT @Street = LocationID
FROM [Location]
WHERE [Location].StreeAddress ='100 White Oak Rd'
INSERT INTO Dealership ([LocationID],[DealerName],[Phone])
VALUES(@Street, 'London', '519-222-4444')
--Select the ID for Kitchener Dealership to be used for all the employees and orders
SELECT @DealID = DealershipID
FROM Dealership
WHERE DealerName = 'Kitchener'
---------------Insert Persons into the Person Table, Customer Table, Employee Table---------------
DECLARE @ManID INT
--Insert John's details in Person, Employee and Salary Table
SELECT @Street = LocationID
FROM [Location]
WHERE [Location].StreeAddress ='22 Queen St'
INSERT INTO Person([FirstName],[LastName],[Phone],[Email],[DateofBirth],[Title],[PerLocID])
VALUES ('John', 'Smith', '519-444-3333', 'jsmtith@email.com', '1968-04-09', 'Mr', @Street)
SELECT @PerID = PersonID
FROM Person
WHERE FirstName = 'John' AND LastName ='Smith'
INSERT INTO [dbo].[Employee]([EmployeeID],[HireDate],[EmpRole],[EmpDealID])
VALUES (@PerID, '2019-01-01', 'Manager',@DealID)
INSERT INTO [dbo].[SalaryEmployee]([EmployeeID],[Salary])
VALUES (@PerID, 95000)
--Insert Mary's details in Person, Employee and Salary Table
SELECT @Street = LocationID
FROM [Location]
WHERE [Location].StreeAddress ='44 King St'
INSERT INTO Person([FirstName],[LastName],[Phone],[Email],[DateofBirth],[Title],[PerLocID])
VALUES ('Mary', 'Brown', '519-888-3333', 'mbrown@email.com', '1972-02-04', 'Ms', @Street)
SELECT @PerID = PersonID
FROM Person
WHERE FirstName = 'Mary' AND LastName ='Brown'
SELECT @ManID = PersonID
FROM Person
WHERE FirstName = 'John' AND LastName ='Smith'
INSERT INTO [dbo].[Employee]([EmployeeID],[HireDate],[EmpRole],[EmpDealID], [ManagerID])
VALUES (@PerID , '2017-04-01', 'Office Admin', @DealID, @ManID)
INSERT INTO [dbo].[SalaryEmployee]([EmployeeID],[Salary])
VALUES (@PerID , 65000)
--Insert Tracy's details in Person, Employee and Salary Table and Customer Table
SELECT @Street = LocationID
FROM [Location]
WHERE [Location].StreeAddress ='55 Krug St'
INSERT INTO Person([FirstName],[LastName],[Phone],[Email],[DateofBirth],[Title],[PerLocID])
VALUES ('Tracy', 'Spencer', '519-888-2222', 'tspencer@email.com', '1998-07-22', 'Ms', @Street)
SELECT @PerID = PersonID
FROM Person
WHERE FirstName = 'Tracy' AND LastName ='Spencer'
INSERT INTO [dbo].[Employee]([EmployeeID],[HireDate],[EmpRole],[EmpDealID], [ManagerID])
VALUES (@PerID, '2019-08-01', 'Sales', @DealID, @ManID)
INSERT INTO [dbo].[CommissionEmployee]([EmployeeID],[Commission])
VALUES (@PerID, '13')
INSERT INTO [dbo].[Customer]([CustomerID],[RegDate])
VALUES (@PerID, '2019-12-01')
--Insert James' details in Person, Employee and Salary Table
SELECT @Street = LocationID
FROM [Location]
WHERE [Location].StreeAddress ='77 Lynn Ct'
INSERT INTO Person([FirstName],[LastName],[Phone],[Email],[DateofBirth],[Title],[PerLocID])
VALUES ('James', 'Stewart', '416-888-1111', 'jstewart@email.com', '1996-11-22', 'Mr', @Street)
SELECT @PerID = PersonID
FROM Person
WHERE FirstName = 'James' AND LastName ='Stewart'
INSERT INTO [dbo].[Employee]([EmployeeID],[HireDate],[EmpRole],[EmpDealID], [ManagerID])
VALUES (@PerID, '2020-01-01', 'Sales', @DealID, @ManID)
INSERT INTO [dbo].[CommissionEmployee]([EmployeeID],[Commission])
VALUES (@PerID, '15')
--Insert Paul's details in Person, Employee and Salary Table
SELECT @Street = LocationID
FROM [Location]
WHERE [Location].StreeAddress ='55 Krug St'
INSERT INTO Person([FirstName],[LastName],[Phone],[Email],[DateofBirth],[Title],[PerLocID])
VALUES ('Paul', 'Newman', '519-888-4444', 'pnewman@email.com', '1992-09-23', 'Mr', @Street)
SELECT @PerID = PersonID
FROM Person
WHERE FirstName = 'Paul' AND LastName ='Newman'
INSERT INTO [dbo].[Employee]([EmployeeID],[HireDate],[EmpRole],[EmpDealID], [ManagerID])
VALUES (@PerID, '2019-01-01', 'Sales', @DealID, @ManID)
INSERT INTO [dbo].[CommissionEmployee]([EmployeeID],[Commission])
VALUES (@PerID, '10')
--Insert Tom's details in Person and Customer Table
SELECT @Street = LocationID
FROM [Location]
WHERE [Location].StreeAddress ='55 Krug St'
INSERT INTO Person([FirstName],[LastName],[Phone],[Email],[DateofBirth],[Title],[PerLocID])
VALUES ('Tom', 'Cruise', '519-333-2222', 'tcruise@email.com', '1962-03-22', 'Mr', @Street)
SELECT @PerID = PersonID
FROM Person
WHERE FirstName = 'Tom' AND LastName ='Cruise'
INSERT INTO [dbo].[Customer]([CustomerID],[RegDate])
VALUES (@PerID, '2020-01-01')
--Insert Bette's details in Person and Customer Table
SELECT @Street = LocationID
FROM [Location]
WHERE [Location].StreeAddress ='99 Lynn Ct'
INSERT INTO Person([FirstName],[LastName],[Phone],[Email],[DateofBirth],[Title],[PerLocID])
VALUES ('Bette', 'Davis', '519-452-1111', 'bdavis@email.com', '1952-09-01', 'Ms', @Street)
SELECT @PerID = PersonID
FROM Person
WHERE FirstName = 'Bette' AND LastName ='Davis'
INSERT INTO [dbo].[Customer]([CustomerID],[RegDate])
VALUES (@PerID, '2020-02-01')
--Insert Grace's details in Person and Customer Table
SELECT @Street = LocationID
FROM [Location]
WHERE [Location].StreeAddress ='88 King St'
INSERT INTO Person([FirstName],[LastName],[Phone],[Email],[DateofBirth],[Title],[PerLocID])
VALUES ('Grace', 'Kelly', '416-887-2222', 'gkelly@email.com', '1973-06-09', 'Ms', @Street)
SELECT @PerID = PersonID
FROM Person
WHERE FirstName = 'Grace' AND LastName ='Kelly'
INSERT INTO [dbo].[Customer]([CustomerID],[RegDate])
VALUES (@PerID, '2020-10-01')
--Insert Doris' details in Person and Customer Table
SELECT @Street = LocationID
FROM [Location]
WHERE [Location].StreeAddress ='44 Cedar St'
INSERT INTO Person([FirstName],[LastName],[Phone],[Email],[DateofBirth],[Title],[PerLocID])
VALUES ('Doris', 'Day', '519-888-5456', 'dday@email.com', '1980-05-25', 'Ms', @Street)
SELECT @PerID = PersonID
FROM Person
WHERE FirstName = 'Doris' AND LastName ='Day'
INSERT INTO [dbo].[Customer]([CustomerID],[RegDate])
VALUES (@PerID, '2020-08-08')
---------------------Insert orders into Order Table-------------------------------------------------
DECLARE @EmpID INT
DECLARE @VehID INT
DECLARE @OrdID INT
--SELECT @DealID = DealershipID FROM Dealership WHERE DealerName = 'Kitchener Dealership'
-----Insert Tom's Order into Order and OrderItem Tables
SELECT @PerID = CustomerID
FROM Customer
JOIN Person ON CustomerID = Person.PersonID
WHERE Person.FirstName = 'Tom'
SELECT @EmpID = EmployeeID
FROM Employee
JOIN Person ON EmployeeID = Person.PersonID
WHERE Person.FirstName = 'Tracy'
INSERT INTO [Order]([OrderCustID],[OrderEmpID],[OrderDate],[OrderDealID])
VALUES (@PerID, @EmpID,'2020-11-01',@DealID)
SELECT @OrdID = OrderID
FROM [ORDER]
JOIN Person ON OrderCustID = Person.PersonID
WHERE Firstname = 'Tom' AND LastName = 'Cruise'
SELECT @VehID = VehicleID
FROM Vehicle
WHERE Make='Toyota' AND Model = 'Corola' AND VehicleYear = '2018' AND Colour = 'Silver'
INSERT INTO OrderItem ([OrderID],[VehicleID],[FinalSalePrice])
VALUES(@OrdID, @VehID,'17500')
SELECT @VehID = VehicleID
FROM Vehicle
WHERE Make='Toyota' AND Model = 'Camry' AND VehicleYear = '2018' AND Colour = 'White'
INSERT INTO OrderItem ([OrderID],[VehicleID],[FinalSalePrice])
VALUES(@OrdID, @VehID,'21000')
-----Insert Bette's Order into Order and OrderItem Tables
SELECT @PerID = CustomerID
FROM Customer
JOIN Person ON CustomerID = Person.PersonID
WHERE Person.FirstName = 'Bette'
INSERT INTO [dbo].[Order]([OrderCustID],[OrderEmpID],[OrderDate],[OrderDealID])
VALUES (@PerID, @EmpID,'2020-11-04',@DealID)
SELECT @OrdID = OrderID
FROM [ORDER]
JOIN Person ON OrderCustID = Person.PersonID
WHERE Firstname = 'Bette' AND LastName = 'Davis'
SELECT @VehID = VehicleID
FROM Vehicle
WHERE Make='Ford' AND Model = 'Focus' AND VehicleYear = '2017' AND Colour = 'Blue'
INSERT INTO OrderItem ([OrderID],[VehicleID],[FinalSalePrice])
VALUES(@OrdID, @VehID,'15000')
-----Insert Tracy's Order into Order and OrderItem Tables
SELECT @PerID = CustomerID
FROM Customer JOIN Person ON CustomerID = Person.PersonID
WHERE Person.FirstName = 'Tracy'
SELECT @EmpID = EmployeeID
FROM Employee
JOIN Person ON EmployeeID = Person.PersonID
WHERE Person.FirstName = 'James'
INSERT INTO [dbo].[Order]([OrderCustID],[OrderEmpID],[OrderDate],[OrderDealID])
VALUES (@PerID, @EmpID,'2020-11-05',@DealID)
SELECT @OrdID = OrderID
FROM [ORDER] JOIN Person ON OrderCustID = Person.PersonID
WHERE Firstname = 'Tracy' AND LastName = 'Spencer'
SELECT @VehID = VehicleID
FROM Vehicle
WHERE Make='Honda' AND Model = 'Acord' AND VehicleYear = '2015' AND Colour = 'Red'
INSERT INTO OrderItem ([OrderID],[VehicleID],[FinalSalePrice])
VALUES(@OrdID, @VehID,'15000')
COMMIT TRANSACTION
END TRY
BEGIN CATCH
ROLLBACK TRANSACTION
INSERT INTO EventLog.dbo.DbErrorLog (UserName, ErrorNumber, ErrorState, ErrorSeverity, ErrorLine, ErrorProcedure, ErrorMessage)
VALUES (SUSER_SNAME(), ERROR_NUMBER(), ERROR_STATE(), ERROR_SEVERITY(), ERROR_LINE(), ERROR_PROCEDURE(), ERROR_MESSAGE());
END CATCH
GO
/*=====================================================================*/
/* Section 3: Lab 7 Create WonderfulWheels Data Warehouse */
/*=====================================================================*/
USE master; --Use Master Database
GO
--Create WonderfulWheelsDW data warehouse, if exist, drop existing databse before creating a new one.
Use master
go
IF DB_ID (N'WonderfulWheelsDW') IS NOT NULL
DROP DATABASE WonderfulWheelsDW;
GO
CREATE DATABASE WonderfulWheelsDW
GO
--UseWonderfulWheels Database from now on.
USE WonderfulWheels;
GO
-- Create and Load CommissionEmployee dimension table
SELECT
IDENTITY(INT,1,1) AS EmployeeSK,
CE.EmployeeID AS EmployeeAK,
P.Title,
P.FirstName,
P.LastName,
P.DateofBirth,
P.Phone,
P.Email,
E.HireDate,
E.EmpRole,
CE.Commission,
CONCAT(M.FirstName,' ', M.LastName) AS Manager
INTO [WonderfulWheelsDW].[dbo].[Dim_CommissionEmployee]
FROM [dbo].[CommissionEmployee] AS CE
JOIN [dbo].[Employee] AS E ON E.EmployeeID = CE.EmployeeID
JOIN [dbo].[Person] AS P on P.PersonID = CE.EmployeeID
JOIN [dbo].[Employee] AS E2 ON E2.EmployeeID = E.ManagerID
JOIN [dbo].[Person] AS M on M.PersonID = E2.EmployeeID
GO
--Alter the table to make EmployeeSK the Primary Key
ALTER TABLE [WonderfulWheelsDW].[dbo].[Dim_CommissionEmployee]
ADD CONSTRAINT PK_DimEmployee
PRIMARY KEY (EmployeeSK)
GO
-- Test
SELECT * FROM [WonderfulWheelsDW].[dbo].[Dim_CommissionEmployee]
GO
-- Create and Load Customer dimension table
SELECT
IDENTITY(INT,1,1) AS CustomerSK,
C.CustomerID AS CustomerAK,
P.Title,
P.FirstName,
P.LastName,
P.DateofBirth,
P.Phone,
P.Email,
C.RegDate
INTO [WonderfulWheelsDW].[dbo].[Dim_Customer]
FROM [dbo].[Customer] AS C
JOIN [dbo].[Person] AS P on P.PersonID = C.CustomerID
GO
--Alter the table to make CustomerSK the Primary Key
ALTER TABLE [WonderfulWheelsDW].[dbo].[Dim_Customer]
ADD CONSTRAINT PK_DimCustomer
PRIMARY KEY (CustomerSK)
GO
-- Test
SELECT * FROM [WonderfulWheelsDW].[dbo].[Dim_Customer]
GO
-- Create and Load Vehicle dimension table
CREATE TABLE [WonderfulWheelsDW].[dbo].[Dim_Vehicle] (
VehicleSK int IDENTITY(1,1) NOT NULL,
VehicleAK int NOT NULL,
Make nvarchar(50) NOT NULL,
Model nvarchar(50) NOT NULL,
VehicleYear int NOT NULL,
Colour nvarchar(10) NOT NULL,
KM int NOT NULL,
Price decimal(12,2) NULL,
CONSTRAINT PK_DimVehicle PRIMARY KEY (VehicleSK)
)
GO
INSERT INTO [WonderfulWheelsDW].[dbo].[Dim_Vehicle] (VehicleAK, Make, Model, VehicleYear, Colour, KM, Price)
SELECT
VehicleID,
Make,
Model,
VehicleYear,
Colour,
KM,
Price
FROM [dbo].[Vehicle]
GO
-- Test
SELECT * FROM [WonderfulWheelsDW].[dbo].[Dim_Vehicle]
GO
-- Create and Load Dealership dimension table
SELECT
IDENTITY(INT,1,1) AS DealershipSK,
DealershipID AS DealershipAK,
DealerName,
Phone,
L.StreeAddress,
L.City,
L.Province,
L.PostalCode
INTO [WonderfulWheelsDW].[dbo].[Dim_Dealership]
FROM [dbo].[Dealership] AS D
JOIN [Location] AS L ON L.LocationID = D.LocationID
GO
--Alter the table to make DealershipSK the Primary Key
ALTER TABLE [WonderfulWheelsDW].[dbo].[Dim_Dealership]
ADD CONSTRAINT PK_DimDealership
PRIMARY KEY (DealershipSK)
GO
-- Test
SELECT * FROM [WonderfulWheelsDW].[dbo].[Dim_Dealership]
GO
-- Create and Load Date dimension table
declare @StartDate date
declare @EndDate date
declare @TempDate date
set @StartDate = '1990-01-01' -- Set this to whatever date you like to begin with
set @EndDate = '2049-12-31' -- Set this to your final date. No date in your systems should exceed this value
declare @Counter int
declare @NumberOfDays int
set @Counter = 0
set @NumberOfDays = datediff(dd,@StartDate,@EndDate)
if not exists (
select *
from sys.tables where name = 'Dim_Date'
)
begin
create table [WonderfulWheelsDW].[dbo].[Dim_Date] (
DateSK int not null,
[Date] date not null,
StandardDate varchar(10) not null,
FullDate varchar(50) not null,
[Year] int not null,
[Month] tinyint not null,
[MonthName] varchar(10) not null,
[Day] tinyint not null,
[DayOfWeek] tinyint not null,
[DayName] varchar(10) not null,
WeekOfYear tinyint not null,
[DayOfYear] smallint not null,
CalendarQuarter tinyint not null
);
end
-- Insert Inferred Member
insert [WonderfulWheelsDW].[dbo].[Dim_Date] (
DateSK,[Date],StandardDate,FullDate,
[Year],[Month],[MonthName],[Day],[DayOfWeek],[DayName],
WeekOfYear,[DayOfYear],CalendarQuarter
) values (
19000101,'1900-01-01','01/01/1900','January 1, 1900',
1900,1,'January',1,2,'Monday',
1,1,1);
set @TempDate = @StartDate
while @Counter <= @NumberOfDays
begin
insert [WonderfulWheelsDW].[dbo].[Dim_Date] (
DateSK,
[Date],
StandardDate,
FullDate,
[Year],
[Month],
[MonthName],
[Day],
[DayOfWeek],
[DayName],
WeekOfYear,
[DayOfYear],
CalendarQuarter
)
select year(@TempDate) * 10000 + month(@TempDate) * 100 + day(@TempDate) as DateSK,
cast(@TempDate as date) as [Date],
convert(varchar,@TempDate,101) as StandardDate,
datename(mm,@TempDate) + ' ' + cast(day(@TempDate) as varchar(2)) + ', ' + cast(year(@TempDate) as char(4)) as FullDate,
year(@TempDate) as [Year],
month(@TempDate) as [Month],
datename(mm,@TempDate) as [MonthName],
day(@TempDate) as [Day],
datepart(dw,@TempDate) as [DayOfWeek],
datename(dw,@TempDate) as [DayName],
datepart(wk,@TempDate) as WeekOfYear,
datepart(dy,@TempDate) as [DayOfYear],
case when month(@TempDate) between 1 and 3 then 1
when month(@TempDate) between 4 and 6 then 2
when month(@TempDate) between 7 and 9 then 3
when month(@TempDate) between 10 and 12 then 4
end as CalendarQuarter
set @Counter = @Counter + 1
set @TempDate = DateAdd(dd,1,@TempDate)
end
if not exists (
select * from sys.indexes
where object_name(object_id) = 'Dim_Date'
and type_desc = 'CLUSTERED'
)
begin
-- Add PK/Clustered Index
alter table [WonderfulWheelsDW].[dbo].[Dim_Date]
add constraint PK_Dim_Date primary key clustered(DateSK);
end
GO
---Test
SELECT * FROM [WonderfulWheelsDW].[dbo].[Dim_Date]
GO
-- Create and Load FactSales Fact table
SELECT DISTINCT
O.OrderID, -- This will become the first PK (Since OrderItem table have composite PK)
OI.VehicleID, --This will become the second PK (Since OrderItem table have composite PK)
ROW_NUMBER() OVER(PARTITION BY O.OrderID ORDER BY OI.OrderID, OI.VehicleID) AS OrderItemID,
CAST (REPLACE (O.OrderDate,'-','') AS int) AS OrderDateSK,
E.EmployeeSK,
C.CustomerSK,
V.VehicleSK,
D.DealershipSK,
OI.FinalSalePrice,
CAST((Commission*FinalSalePrice) AS decimal(12,2)) AS CommissionAmount
INTO [WonderfulWheelsDW].[dbo].[Fact_Sales]
FROM [dbo].[Order] AS O
JOIN [dbo].[OrderItem] AS OI ON OI.OrderID= O.OrderID
LEFT JOIN [WonderfulWheelsDW].[dbo].[Dim_CommissionEmployee] as E ON E.EmployeeAK=O.OrderEmpID
LEFT JOIN [WonderfulWheelsDW].[dbo].[Dim_Customer] as C ON C.CustomerAK=O.OrderCustID
LEFT JOIN [WonderfulWheelsDW].[dbo].[Dim_Vehicle]as V ON V.VehicleAK=OI.VehicleID
LEFT JOIN [WonderfulWheelsDW].[dbo].[Dim_Dealership] AS D ON D.DealershipAK=O.OrderDealID
GO
ALTER TABLE [WonderfulWheelsDW].[dbo].[Fact_Sales]
ALTER COLUMN OrderItemID INT NOT NULL
GO
--Alter the table to make OrderID and VehicleID the Primary Key
ALTER TABLE [WonderfulWheelsDW].[dbo].[Fact_Sales]
ADD CONSTRAINT PK_FactSales
PRIMARY KEY (OrderID, OrderItemID)
GO
--Alter the table to make DelaershipSK the Foreign Key
ALTER TABLE [WonderfulWheelsDW].[dbo].[Fact_Sales]
ADD CONSTRAINT FK_FactSales_DimDealership
FOREIGN KEY (DealershipSK) REFERENCES Dim_Dealership(DealershipSK)
GO
--Alter the table to make CustomerSK the Foreign Key
ALTER TABLE [WonderfulWheelsDW].[dbo].[Fact_Sales]
ADD CONSTRAINT FK_FactSales_DimCustomer
FOREIGN KEY (CustomerSK) REFERENCES Dim_Customer(CustomerSK)
GO
--Alter the table to make EmployeeSK the Foreign Key
ALTER TABLE [WonderfulWheelsDW].[dbo].[Fact_Sales]
ADD CONSTRAINT FK_FactSales_DimEmployee
FOREIGN KEY (EmployeeSK) REFERENCES Dim_CommissionEmployee(EmployeeSK)
GO
--Alter the table to make VehicleSK the Foreign Key
ALTER TABLE [WonderfulWheelsDW].[dbo].[Fact_Sales]
ADD CONSTRAINT FK_FactSales_DimVehicle
FOREIGN KEY (VehicleSK) REFERENCES Dim_Vehicle(VehicleSK)
GO
--Alter the table to make OrderItemSK the Foreign Key
ALTER TABLE [WonderfulWheelsDW].[dbo].[Fact_Sales]
ADD CONSTRAINT FK_FactSales_DimDate
FOREIGN KEY (OrderDateSK) REFERENCES [WonderfulWheelsDW].[dbo].[Dim_Date](DateSK)
GO
-- Test
SELECT * FROM [WonderfulWheelsDW].[dbo].[Fact_Sales]
GO
use WonderfulWheelsDW
go
create view dbo.vwGetEmployeeSales
as
select e.FirstName,e.LastName,e.Email,
(DATEDIFF(year,e.DateofBirth,getdate()))as age,
(DATEDIFF(year,e.HireDate,getdate())) as [YearsOfservice],
e.Commission as [CommissionPercentage],
isnull(cast(f.CommissionAmount as varchar),'')as [SaleCommission],
isnull((c.FirstName + ' ' + c.LastName),'') as [CustomerName],
isnull(c.RegDate,'')as RegistartionDate,
isnull(c.Title,'') as Title,
isnull(d.DealerName,'')as DelarshipName,
isnull(d.StreeAddress,'')as streetaddress,
isnull(d.City,'')as CityName,
isnull(d.Province,'')as ProvinceName,
isnull(v.Make,'')as Make,
isnull(v.Model,'')as Model,
isnull(v.VehicleYear,'')as [MakeYear],
isnull(v.KM,'')as KM,
isnull(cast(v.Price as varchar),'')as Price,
isnull(cast(FinalSalePrice as varchar),'')as FinalSalePrice,
isnull(dt.Year,'') as Year,
isnull(dt.CalendarQuarter,'') as CalendarQuarter,
isnull(dt.Month,'')as Month
from WonderfulWheelsDW.dbo.Dim_CommissionEmployee as e
left join WonderfulWheelsDW.dbo.Fact_Sales as f on e.EmployeeSK =f.EmployeeSK
left join WonderfulWheelsDW.dbo.Dim_Customer as c on c.CustomerSK =f.CustomerSK
left join WonderfulWheelsDW.dbo.Dim_Dealership as d on d.DealershipSK =f.DealershipSK
left join WonderfulWheelsDW.dbo.Dim_Vehicle as v on v.VehicleSK =f.VehicleSK
left join WonderfulWheelsDW.dbo.Dim_Date as dt on dt.DateSK =f.OrderDateSK
go
declare @FirstName varchar(50),
@LastName Varchar(50),
@Year int,
@Month int
set @FirstName=''
set @LastName =''
set @Year = null
set @Month = null
select * from dbo.vwGetEmployeeSales
where(FirstName=@FirstName or @FirstName='')
and (LastName = @LastName or @LastName='')
and (Year=@Year or @Year is null)
and (Month=@Month or @Month is null)
order by FirstName, LastName
go
-- creating SP
create procedure dbo.spGetEmployeeSales
(
@FirstName varchar(50),
@LastName Varchar(50),
@Year int,
@Month int
)
as
begin
Select * from dbo.vwGetEmployeeSales
where(FirstName like '%'+@FirstName+'%' or @FirstName='')
and (LastName like '%'+@LastName+'%' or @LastName='')
and (Year=@Year or @Year is null)
and (Month=@Month or @Month is null)
order by FirstName, LastName
end
go
-- year for drop down list
select distinct null as value,'All' as label, 0 as sortOrder
union
select distinct [year] as value, cast([YEAR] as varchar(4)) as label ,1
from WonderfulWheelsDW.dbo.dim_Date
order by sortOrder,label
-- month for DropDown List
select distinct null as value,'All' as label, 0 as sortOrder
union
select distinct [Month] as value, cast([MONTH] as varchar(2)) as label ,1
from WonderfulWheelsDW.dbo.dim_Date
order by sortOrder,label
EXEC dbo.spGetEmployeeSales @FirstName = '', @LastName = '', @Year=0 , @Month =0;