-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDML.sql
More file actions
676 lines (602 loc) · 18.6 KB
/
Copy pathDML.sql
File metadata and controls
676 lines (602 loc) · 18.6 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
--=============================================Insert Section===============================
-- Insert records into Owners table
USE ResidentialBusiness;
GO
INSERT INTO rb.Owners (OwnerID, OwnerName, ContactNumber, Email)
VALUES
(1, 'Abul Kalam', '01711111111', 'abul.kalam@gmail.com'),
(2, 'Salma Akter', '01811111112', 'salma.akter@gmail.com'),
(3, 'Faruk Ahmed', '01911111113', 'faruk.ahmed@gmail.com'),
(4, 'Khaleda Begum', '01711111114', 'khaleda.begum@gmail.com'),
(5, 'Nurul Haque', '01811111115', 'nurul.haque@gmail.com'),
(6, 'Hasina Parveen', '01911111116', 'hasina.parveen@gmail.com'),
(7, 'Anisur Rahman', '01711111117', 'anisur.rahman@gmail.com'),
(8, 'Tahmina Sultana', '01811111118', 'tahmina.sultana@gmail.com'),
(9, 'Rezaul Karim', '01911111119', 'rezaul.karim@gmail.com'),
(10, 'Shahnaz Rahman', '01711111120', 'shahnaz.rahman@gmail.com');
-- Insert records into Properties table
USE ResidentialBusiness;
GO
INSERT INTO rb.Properties (PropertyID, PropertyName, [Location], Price, [Description], SquareFootage)
VALUES
(1, 'Green Valley', 'Dhaka', 20000000, 'Modern Apartment', 1500),
(2, 'Sunshine Villa', 'Chittagong', 18000000, 'Luxury House', 1800),
(3, 'Lakeview Tower', 'Sylhet', 25000000, 'Scenic Apartment', 2000),
(4, 'Rose Garden', 'Khulna', 12000000, 'Affordable Flat', 1200),
(5, 'Ocean Breeze', 'Cox''s Bazar', 30000000, 'Sea View', 2200),
(6, 'Hill Top', 'Bandarban', 16000000, 'Mountain Retreat', 1400),
(7, 'City Heights', 'Dhaka', 27000000, 'Prime Location', 1700),
(8, 'Golden Horizon', 'Rajshahi', 15000000, 'Quiet Neighborhood', 1300),
(9, 'Riverside', 'Barisal', 20000000, 'Riverside Apartment', 1800),
(10, 'Forest View', 'Sylhet', 22000000, 'Nature Inspired', 1900);
-- Insert records into LeasesFromOwner table
USE ResidentialBusiness;
GO
INSERT INTO rb.LeasesFromOwner (LeaseFromOwnerID, OwnerID, PropertyID, StartDate, EndDate, Rent)
VALUES
(1, 1, 1, '2023-01-01', '2023-12-31', 50000),
(2, 2, 2, '2023-02-01', '2023-12-31', 45000),
(3, 3, 3, '2023-03-01', '2023-12-31', 55000),
(4, 4, 4, '2023-04-01', '2023-12-31', 35000),
(5, 5, 5, '2023-05-01', '2023-12-31', 70000),
(6, 6, 6, '2023-06-01', '2023-12-31', 40000),
(7, 7, 7, '2023-07-01', '2023-12-31', 65000),
(8, 8, 8, '2023-08-01', '2023-12-31', 30000),
(9, 9, 9, '2023-09-01', '2023-12-31', 60000),
(10, 10, 10, '2023-10-01', '2023-12-31', 55000);
-- Insert records into Apartments table
USE ResidentialBusiness;
GO
INSERT INTO rb.Apartments (ApartmentID, PropertyID, FloorNumber, UnitNumber)
VALUES
(1, 1, 2, 'A-201'),
(2, 2, 3, 'B-301'),
(3, 3, 1, 'C-101'),
(4, 4, 2, 'D-202'),
(5, 5, 4, 'E-401'),
(6, 6, 1, 'F-102'),
(7, 7, 3, 'G-303'),
(8, 8, 2, 'H-204'),
(9, 9, 1, 'I-105'),
(10, 10, 3, 'J-306');
-- Insert records into Residents table
USE ResidentialBusiness;
GO
INSERT INTO rb.Residents (ResidentID, ResidentName, ContactNumber, Email)
VALUES
(1, 'Rafiqul Islam', '01611111121', 'rafiqul.islam@gmail.com'),
(2, 'Momena Khatun', '01711111122', 'momena.khatun@gmail.com'),
(3, 'Jahangir Alam', '01811111123', 'jahangir.alam@gmail.com'),
(4, 'Shamim Ara', '01911111124', 'shamim.ara@gmail.com'),
(5, 'Karim Ullah', '01611111125', 'karim.ullah@gmail.com'),
(6, 'Nazma Banu', '01711111126', 'nazma.banu@gmail.com'),
(7, 'Mokhlesur Rahman', '01811111127', 'mokhlesur.rahman@gmail.com'),
(8, 'Taher Ali', '01911111128', 'taher.ali@gmail.com'),
(9, 'Sabina Yasmin', '01611111129', 'sabina.yasmin@gmail.com'),
(10, 'Selim Ahmed', '01711111130', 'selim.ahmed@gmail.com');
-- Insert records into LeasesToResidents table
USE ResidentialBusiness;
GO
INSERT INTO rb.LeasesToResidents (LeaseToResidentID, ApartmentID, ResidentID, StartDate, EndDate, Rent)
VALUES
(1, 1, 1, '2023-01-01', '2023-12-31', 20000),
(2, 2, 2, '2023-02-01', '2023-12-31', 18000),
(3, 3, 3, '2023-03-01', '2023-12-31', 22000),
(4, 4, 4, '2023-04-01', '2023-12-31', 15000),
(5, 5, 5, '2023-05-01', '2023-12-31', 30000),
(6, 6, 6, '2023-06-01', '2023-12-31', 16000),
(7, 7, 7, '2023-07-01', '2023-12-31', 25000),
(8, 8, 8, '2023-08-01', '2023-12-31', 14000),
(9, 9, 9, '2023-09-01', '2023-12-31', 20000),
(10, 10, 10, '2023-10-01', '2023-12-31', 22000)
-- Insert records into MaintenanceRequests table
USE ResidentialBusiness;
GO
INSERT INTO rb.MaintenanceRequests (RequestID, ApartmentID, ResidentID, [Status], Descriptions)
VALUES
(1, 1, 1, 'Pending', 'Leakage'),
(2, 2, 2, 'Completed', 'Electrical Issue'),
(3, 3, 3, 'In Progress', 'Broken Door'),
(4, 4, 4, 'Pending', 'Plumbing Issue'),
(5, 5, 5, 'Completed', 'Painting'),
(6, 6, 6, 'In Progress', 'AC Repair'),
(7, 7, 7, 'Pending', 'Gas Line Issue'),
(8, 8, 8, 'Completed', 'Window Replacement'),
(9, 9, 9, 'In Progress', 'Water Supply'),
(10, 10, 10, 'Pending', 'Flooring');
-- Insert records into Employees table
USE ResidentialBusiness;
GO
INSERT INTO rb.Employees (EmployeeID, [Name], Position, ContactNumber, Email, HireDate, Salary)
VALUES
(1, 'Mehedi Hasan', 'Manager', '01722222221', 'mehedi.hasan@gmail.com', '2021-03-15', 50000),
(2, 'Sumi Akter', 'Accountant', '01822222222', 'sumi.akter@gmail.com', '2022-06-10', 40000),
(3, 'Nazmul Huda', 'Technician', '01922222223', 'nazmul.huda@gmail.com', '2021-11-01', 35000),
(4, 'Tania Rahman', 'Receptionist', '01722222224', 'tania.rahman@gmail.com', '2022-01-25', 30000),
(5, 'Fahim Hossain', 'Supervisor', '01822222225', 'fahim.hossain@gmail.com', '2021-07-20', 45000),
(6, 'Anika Sultana', 'Technician', '01922222226', 'anika.sultana@gmail.com', '2021-09-30', 35000),
(7, 'Rashed Mahmud', 'Security', '01722222227', 'rashed.mahmud@gmail.com', '2022-03-15', 25000),
(8, 'Shahidul Islam', 'Cleaner', '01822222228', 'shahidul.islam@gmail.com', '2021-04-10', 20000),
(9, 'Mili Akter', 'Office Assistant', '01922222229', 'mili.akter@gmail.com', '2022-08-05', 25000),
(10, 'Abdullah Al Mamun', 'Technician', '01722222230', 'abdullah.mamun@gmail.com', '2021-05-20', 35000);
-- Insert records into Buy table
USE ResidentialBusiness;
GO
INSERT INTO rb.Buy (PaymentID, LeaseFromOwnerID, OwnerID, PropertyID, PaymentDate, PaymentMethod, Amount, EmployeeSalary)
VALUES
(1, 1, 1, 1, '2023-01-15', 'Cash', 2000000, 50000),
(2, 2, 2, 2, '2023-02-10', 'Bank Transfer', 1800000, 40000),
(3, 3, 3, 3, '2023-03-05', 'Cheque', 2500000, 35000),
(4, 4, 4, 4, '2023-04-20', 'Cash', 1200000, 30000),
(5, 5, 5, 5, '2023-05-25', 'Bank Transfer', 3000000, 45000),
(6, 6, 6, 6, '2023-06-30', 'Cheque', 1600000, 35000),
(7, 7, 7, 7, '2023-07-15', 'Cash', 2700000, 45000),
(8, 8, 8, 8, '2023-08-10', 'Bank Transfer', 1500000, 25000),
(9, 9, 9, 9, '2023-09-05', 'Cheque', 2000000, 20000),
(10, 10, 10, 10, '2023-10-20', 'Cash', 2200000, 35000);
-- Insert records into Sales table
USE ResidentialBusiness;
GO
INSERT INTO rb.Sales (ResidencePaymentID, LeaseToResidentID, ApartmentID, ResidentID, PaymentDate, PaymentMethod, Amount)
VALUES
(1, 1, 1, 1, '2023-01-10', 'Cash', 20000),
(2, 2, 2, 2, '2023-02-12', 'Bank Transfer', 18000),
(3, 3, 3, 3, '2023-03-08', 'Cheque', 22000),
(4, 4, 4, 4, '2023-04-15', 'Cash', 15000),
(5, 5, 5, 5, '2023-05-20', 'Bank Transfer', 30000),
(6, 6, 6, 6, '2023-06-18', 'Cheque', 16000),
(7, 7, 7, 7, '2023-07-22', 'Cash', 25000),
(8, 8, 8, 8, '2023-08-25', 'Bank Transfer', 14000),
(9, 9, 9, 9, '2023-09-28', 'Cheque', 20000),
(10, 10, 10, 10, '2023-10-30', 'Cash', 22000);
-- Insert records into ProfitMargin table
USE ResidentialBusiness;
GO
INSERT INTO rb.ProfitMargin (ProfitMarginID, Month, Year, GrossProfit, Expenses)
VALUES
(1, 1, 2023, 1000000, 500000),
(2, 2, 2023, 1200000, 600000),
(3, 3, 2023, 1500000, 700000),
(4, 4, 2023, 1400000, 650000),
(5, 5, 2023, 1600000, 800000),
(6, 6, 2023, 1700000, 850000),
(7, 7, 2023, 2000000, 1000000),
(8, 8, 2023, 1900000, 950000),
(9, 9, 2023, 1800000, 900000),
(10, 10, 2023, 2100000, 1050000);
go
----==================================================Select Section==================
-- Owners
SELECT * FROM rb.Owners;
GO
-- Properties
SELECT * FROM rb.Properties;
GO
-- LeasesFromOwner
SELECT * FROM rb.LeasesFromOwner;
GO
-- Apartments
SELECT * FROM rb.Apartments;
GO
-- Residents
SELECT * FROM rb.Residents;
GO
-- LeasesToResidents
SELECT * FROM rb.LeasesToResidents;
GO
-- MaintenanceRequests
SELECT * FROM rb.MaintenanceRequests;
GO
-- Employees
SELECT * FROM rb.Employees;
GO
-- Buy
SELECT * FROM rb.Buy;
GO
-- Sales
SELECT * FROM rb.Sales;
GO
-- ProfitMargin
SELECT * FROM rb.ProfitMargin;
GO
-- Owners_Log
SELECT * FROM rb.Owners_Log
GO
-- View
SELECT * FROM rb.vw_OwnerDetails;
----=================================================Innser Join==================
USE ResidentialBusiness;
GO
SELECT O.OwnerID, O.OwnerName, P.PropertyName
FROM rb.Owners O
INNER JOIN rb.LeasesFromOwner LFO ON O.OwnerID = LFO.OwnerID
INNER JOIN rb.Properties P ON LFO.PropertyID = P.PropertyID;
go
----==================================================Left Join==================
USE ResidentialBusiness;
GO
SELECT O.OwnerID, O.OwnerName, P.PropertyName
FROM rb.Owners O
LEFT JOIN rb.LeasesFromOwner LFO ON O.OwnerID = LFO.OwnerID
LEFT JOIN rb.Properties P ON LFO.PropertyID = P.PropertyID;
go
----==================================================Right Join==================
USE ResidentialBusiness;
GO
SELECT P.PropertyID, P.PropertyName, O.OwnerName
FROM rb.Properties P
RIGHT JOIN rb.LeasesFromOwner LFO ON P.PropertyID = LFO.PropertyID
RIGHT JOIN rb.Owners O ON LFO.OwnerID = O.OwnerID;
go
----===================================================Full Join==================
USE ResidentialBusiness;
GO
SELECT O.OwnerID, O.OwnerName, P.PropertyName
FROM rb.Owners O
FULL JOIN rb.LeasesFromOwner LFO ON O.OwnerID = LFO.OwnerID
FULL JOIN rb.Properties P ON LFO.PropertyID = P.PropertyID;
----==================================================Cross Join==================
USE ResidentialBusiness;
GO
SELECT O.OwnerID, O.OwnerName, P.PropertyID, P.PropertyName
FROM rb.Owners O
CROSS JOIN rb.Properties P;
go
---==================================================Function Section======================
-----Aggregate Functions
--1. Total Salary for All Employees
USE ResidentialBusiness;
GO
SELECT SUM(Salary) AS TotalSalary
FROM rb.Employees;
go
--2. Average Rent Paid by Residents
USE ResidentialBusiness;
GO
SELECT SUM(Salary) AS TotalSalary
FROM rb.Employees;
go
--3. Maximum and Minimum Property Price
USE ResidentialBusiness;
GO
SELECT MAX(Price) AS MaximumPrice, MIN(Price) AS MinimumPrice
FROM rb.Properties;
go
---4. Total Profit and Average Profit for the Year 2023
USE ResidentialBusiness;
GO
SELECT
SUM(NetProfit) AS TotalProfit,
AVG(NetProfit) AS AverageProfit
FROM rb.ProfitMargin
WHERE Year = 2023;
go
--5. Count of Apartments Per Property
USE ResidentialBusiness;
GO
SELECT PropertyID, COUNT(ApartmentID) AS TotalApartments
FROM rb.Apartments
GROUP BY PropertyID;
go
---6. Total Revenue Collected from Sales
USE ResidentialBusiness;
GO
SELECT SUM(Amount) AS TotalRevenue
FROM rb.Sales;
go
---7. Count of Maintenance Requests by Status
USE ResidentialBusiness;
GO
SELECT [Status], COUNT(RequestID) AS TotalRequests
FROM rb.MaintenanceRequests
GROUP BY [Status];
go
---8. Total Expenditure on Property Purchases (Buy Table)
USE ResidentialBusiness;
GO
SELECT SUM(TotalExpenditure) AS TotalExpenditure
FROM rb.Buy;
go
---9. Total Number of Residents
USE ResidentialBusiness;
GO
SELECT COUNT(ResidentID) AS TotalResidents
FROM rb.Residents;
go
---10. Monthly Average Rent Paid by Residents
USE ResidentialBusiness;
GO
SELECT
Month(StartDate) AS RentMonth,
AVG(Rent) AS AverageMonthlyRent
FROM rb.LeasesToResidents
GROUP BY Month(StartDate)
ORDER BY RentMonth;
go
----11. Total Revenue and Expenses by Month from ProfitMargin
USE ResidentialBusiness;
GO
SELECT
[MONTH],
SUM(GrossProfit) AS TotalRevenue,
SUM(Expenses) AS TotalExpenses
FROM rb.ProfitMargin
GROUP BY [Month]
ORDER BY [Month];
go
---12. Top 3 Highest Paid Employees
USE ResidentialBusiness;
GO
SELECT TOP 3 [Name], Position, Salary
FROM rb.Employees
ORDER BY Salary DESC;
go
--13. Most Expensive Property
USE ResidentialBusiness;
GO
SELECT PropertyName, [Location], Price
FROM rb.Properties
WHERE Price = (SELECT MAX(Price) FROM rb.Properties);
GO
---14. Number of Apartments Available in Each Location
USE ResidentialBusiness;
GO
SELECT p.[Location], COUNT(a.ApartmentID) AS TotalApartments
FROM rb.Properties p
JOIN rb.Apartments a ON p.PropertyID = a.PropertyID
GROUP BY p.[Location];
go
---15. Count of Maintenance Requests Per Resident
USE ResidentialBusiness;
GO
SELECT r.ResidentName, COUNT(m.RequestID) AS TotalRequests
FROM rb.MaintenanceRequests m
JOIN rb.Residents r ON m.ResidentID = r.ResidentID
GROUP BY r.ResidentName
ORDER BY TotalRequests DESC;
go
--=================================================Date and Time Functions===============
--using Getdate(), Dateadd(), Datediff()
use ResidentialBusiness
go
select getdate() as CurrentDateTime
go
use ResidentialBusiness
go
select EmployeeID, [Name], HireDate, dateadd(year,1,HireDate) as HireDateNextYear
from rb.Employees
go
use ResidentialBusiness
go
select LeasesToResidentID, ApartmentID, ResidentID, StartDate, EndDate, datediff(day, StartDay, EndDay) as LeaseDurationDays
from rb.LeasesToResidents
go
--==================================================Scalar Functions===============
--using Len(), Substring(), Convert(), Cast()
USE ResidentialBusiness;
GO
--To know the length of every owner
SELECT OwnerID, OwnerName, LEN(OwnerName) AS NameLength
FROM rb.Owners;
GO
USE ResidentialBusiness;
GO
--To see the first 5 letter of every property
SELECT PropertyID, PropertyName, SUBSTRING(PropertyName, 1, 5) AS ShortName
FROM rb.Properties;
GO
USE ResidentialBusiness;
GO
-- to convert each employee's appointment date to VARCHAR
SELECT EmployeeID, [Name], HireDate, CONVERT(VARCHAR, HireDate, 101) AS HireDateString
FROM rb.Employees;
GO
USE ResidentialBusiness;
GO
-- to convert each property value to FLOAT type
SELECT PropertyID, PropertyName, Price, CAST(Price AS FLOAT) AS PriceAsFloat
FROM rb.Properties;
GO
--================================================String Functions===============
--Using REPLACE(), REVERSE(), LTRIM(), RTRIM()
USE ResidentialBusiness;
GO
-- To replace 'Apartment' with 'Apt' in PropertyName field
SELECT PropertyID, PropertyName, REPLACE(PropertyName, 'Apartment', 'Apt') AS UpdatedPropertyName
FROM rb.Properties;
GO
USE ResidentialBusiness;
GO
--To reverse the characters in the OwnerName field
SELECT OwnerID, OwnerName, REVERSE(OwnerName) AS ReversedOwnerName
FROM rb.Owners;
GO
USE ResidentialBusiness;
GO
--To remove spaces at the beginning of the ResidentName field
SELECT ResidentID, ResidentName, LTRIM(ResidentName) AS TrimmedResidentName
FROM rb.Residents;
GO
USE ResidentialBusiness;
GO
--to remove spaces at the end of the ResidentName field
SELECT ResidentID, ResidentName, RTRIM(ResidentName) AS TrimmedResidentName
FROM rb.Residents;
GO
--===============================================Math Functions===============
--Using ROUND(), ABS(), CEILING(), FLOOR()
USE ResidentialBusiness;
GO
-- To round each property value number to two decimal places
SELECT PropertyID, PropertyName, Price, ROUND(Price, 2) AS RoundedPrice
FROM rb.Properties;
GO
USE ResidentialBusiness;
GO
-- To show the original value of rent for each lease
SELECT LeaseFromOwnerID, Rent, ABS(Rent) AS AbsoluteRent
FROM rb.LeasesFromOwner;
GO
USE ResidentialBusiness;
GO
-- to show the nearest whole number above each property value
SELECT PropertyID, PropertyName, Price, CEILING(Price) AS CeilingPrice
FROM rb.Properties;
GO
USE ResidentialBusiness;
GO
-- to show the nearest whole number below each property value
SELECT PropertyID, PropertyName, Price, FLOOR(Price) AS FloorPrice
FROM rb.Properties;
GO
--===============================================Window Functions==================
--Using ROW_NUMBER(), RANK(), DENSE_RANK(), LEAD(), LAG()
USE ResidentialBusiness;
GO
SELECT
PropertyID,
PropertyName,
Price,
ROW_NUMBER() OVER (ORDER BY Price DESC) AS RowNumber
FROM rb.Properties;
GO
USE ResidentialBusiness;
GO
SELECT
PropertyID,
PropertyName,
Price,
RANK() OVER (ORDER BY Price DESC) AS Rank
FROM rb.Properties;
GO
USE ResidentialBusiness;
GO
SELECT
PropertyID,
PropertyName,
Price,
DENSE_RANK() OVER (ORDER BY Price DESC) AS DenseRank
FROM rb.Properties;
GO
USE ResidentialBusiness;
GO
SELECT
PropertyID,
PropertyName,
Price,
LEAD(Price, 1) OVER (ORDER BY Price DESC) AS NextPrice
FROM rb.Properties;
GO
USE ResidentialBusiness;
GO
SELECT
PropertyID,
PropertyName,
Price,
LAG(Price, 1) OVER (ORDER BY Price DESC) AS PreviousPrice
FROM rb.Properties;
GO
------====================================================CTE Section=============
---CTE in different situation
--1. CTE to Find Total Revenue and Expenses for Each Month
USE ResidentialBusiness;
GO
WITH MonthlyProfit AS (
SELECT
Month,
SUM(GrossProfit) AS TotalRevenue,
SUM(Expenses) AS TotalExpenses
FROM rb.ProfitMargin
GROUP BY Month
)
SELECT
Month,
TotalRevenue,
TotalExpenses,
(TotalRevenue - TotalExpenses) AS NetProfit
FROM MonthlyProfit;
go
--2. CTE to Find Apartments with Rents Higher than Average
USE ResidentialBusiness;
GO
WITH AverageRent AS (
SELECT AVG(Rent) AS AvgRent
FROM rb.LeasesToResidents
)
SELECT
a.ApartmentID,
a.UnitNumber,
l.Rent,
ar.AvgRent
FROM rb.Apartments a
JOIN rb.LeasesToResidents l ON a.ApartmentID = l.ApartmentID
CROSS JOIN AverageRent ar
WHERE l.Rent > ar.AvgRent;
---3. CTE to Calculate Total Salary Paid to Employees
USE ResidentialBusiness;
GO
WITH EmployeeSalaries AS (
SELECT
Position,
SUM(Salary) AS TotalSalary
FROM rb.Employees
GROUP BY Position
)
SELECT
Position,
TotalSalary
FROM EmployeeSalaries;
--4. CTE to Find Top 5 Properties by Price
USE ResidentialBusiness;
GO
WITH TopProperties AS (
SELECT
PropertyID,
PropertyName,
Price,
ROW_NUMBER() OVER (ORDER BY Price DESC) AS Rank
FROM rb.Properties
)
SELECT
PropertyID,
PropertyName,
Price
FROM TopProperties
WHERE Rank <= 5;
--5. CTE to Identify Maintenance Requests Pending by Resident
USE ResidentialBusiness;
GO
WITH PendingRequests AS (
SELECT
m.ResidentID,
r.ResidentName,
COUNT(m.RequestID) AS TotalPending
FROM rb.MaintenanceRequests m
JOIN rb.Residents r ON m.ResidentID = r.ResidentID
WHERE m.[Status] = 'Pending'
GROUP BY m.ResidentID, r.ResidentName
)
SELECT
ResidentID,
ResidentName,
TotalPending
FROM PendingRequests
ORDER BY TotalPending DESC;
--6. CTE to Calculate Total Expenditure by Each Owner
USE ResidentialBusiness;
GO
WITH OwnerExpenditure AS (
SELECT
b.OwnerID,
o.OwnerName,
SUM(b.TotalExpenditure) AS TotalExpenditure
FROM rb.Buy b
JOIN rb.Owners o ON b.OwnerID = o.OwnerID
GROUP BY b.OwnerID, o.OwnerName
)
SELECT
OwnerID,
OwnerName,
TotalExpenditure
FROM OwnerExpenditure
ORDER BY TotalExpenditure DESC;