Bulk: Einheit 1-3
This commit is contained in:
137
Einheit_2/2025_5BHITN_Konstantin_Hintermayer_seed_database.sql
Normal file
137
Einheit_2/2025_5BHITN_Konstantin_Hintermayer_seed_database.sql
Normal file
@@ -0,0 +1,137 @@
|
||||
-- Insert test data into the Location table
|
||||
INSERT INTO [dbo].[Location] ([name], [address])
|
||||
VALUES
|
||||
('Downtown Office', '123 Main St, Cityville'),
|
||||
('Airport Terminal', '456 Airport Rd, Cityville'),
|
||||
('City Center', '789 Central Ave, Cityville'),
|
||||
('Suburban Outlet', '321 Suburb St, Cityville'),
|
||||
('Train Station', '654 Rail Rd, Cityville');
|
||||
|
||||
-- Insert test data into the VehicleCategory table
|
||||
INSERT INTO VehicleCategory ([description], [pricePerDay])
|
||||
VALUES
|
||||
('Compact Car', 29.99),
|
||||
('SUV', 49.99),
|
||||
('Luxury Sedan', 89.99),
|
||||
('Minivan', 59.99),
|
||||
('Pickup Truck', 69.99);
|
||||
|
||||
-- Insert test data into the Vehicle table
|
||||
INSERT INTO Vehicle (brand, model, licensePlate, buyDate, locationID, vehicleCategoryID)
|
||||
VALUES
|
||||
('Toyota', 'Corolla', 'ABC1234', '2020-06-15', 1, 1),
|
||||
('Honda', 'CR-V', 'XYZ5678', '2019-03-22', 2, 2),
|
||||
('BMW', '5 Series', 'LMN9012', '2021-01-10', 3, 3),
|
||||
('Ford', 'Transit', 'GHI3456', '2020-11-05', 4, 4),
|
||||
('Chevrolet', 'Silverado', 'JKL7890', '2018-07-19', 5, 5);
|
||||
|
||||
-- Insert test data into the Customer table
|
||||
INSERT INTO Customer (name, Adress, driverlicenseid)
|
||||
VALUES
|
||||
('John Doe', '1 Elm St, Cityville', 'D123456789'),
|
||||
('Jane Smith', '2 Oak St, Cityville', 'D987654321'),
|
||||
('Bob Johnson', '3 Pine St, Cityville', 'D456123789'),
|
||||
('Alice Williams', '4 Maple St, Cityville', 'D654789123'),
|
||||
('Charlie Brown', '5 Cedar St, Cityville', 'D321654987');
|
||||
|
||||
|
||||
GO
|
||||
-- ########################################################
|
||||
-- # Views #
|
||||
-- ########################################################
|
||||
-- View: v_VerfuegbareFahrzeuge
|
||||
-- Zeigt Fahrzeuge an, die nicht vermietet sind oder deren Rückgabedatum in der Vergangenheit liegt
|
||||
CREATE OR ALTER VIEW v_VerfuegbareFahrzeuge AS
|
||||
SELECT
|
||||
v.id,
|
||||
v.brand,
|
||||
v.model,
|
||||
v.licensePlate,
|
||||
vc.description AS category,
|
||||
vc.pricePerDay,
|
||||
l.name AS location,
|
||||
l.address AS locationAddress,
|
||||
CASE
|
||||
WHEN r.id IS NULL THEN 'Available'
|
||||
WHEN r.rentalEnd < CAST(GETDATE() AS DATE) THEN 'Available (Overdue Return)'
|
||||
ELSE 'Not Available'
|
||||
END AS status
|
||||
FROM Vehicle v
|
||||
JOIN VehicleCategory vc ON v.vehicleCategoryID = vc.id
|
||||
JOIN Location l ON v.locationID = l.id
|
||||
LEFT JOIN Rental r ON v.id = r.vehicleID AND r.actualEnd IS NULL
|
||||
WHERE r.id IS NULL OR r.actualEnd < CAST(GETDATE() AS DATE);
|
||||
GO
|
||||
-- View: v_UmsatzProKategorie
|
||||
-- Zeigt den kumulierten Umsatz pro Fahrzeugkategorie an
|
||||
CREATE OR ALTER VIEW v_UmsatzProKategorie AS
|
||||
SELECT
|
||||
vc.id,
|
||||
vc.description AS category,
|
||||
vc.pricePerDay,
|
||||
COUNT(r.id) AS totalRentals,
|
||||
SUM(
|
||||
DATEDIFF(DAY, r.rentalStart, COALESCE(r.rentalEnd, CAST(GETDATE() AS DATE)))
|
||||
* vc.pricePerDay
|
||||
) AS totalRevenue
|
||||
FROM VehicleCategory vc
|
||||
LEFT JOIN Vehicle v ON vc.id = v.vehicleCategoryID
|
||||
LEFT JOIN Rental r ON v.id = r.vehicleID
|
||||
GROUP BY
|
||||
vc.id,
|
||||
vc.description,
|
||||
vc.pricePerDay;
|
||||
GO
|
||||
|
||||
|
||||
-- ########################################################
|
||||
-- # User Defined Functions #
|
||||
-- ########################################################
|
||||
|
||||
-- Scalar Function: fn_BerechneMietkosten
|
||||
CREATE OR ALTER FUNCTION fn_BerechneMietkosten (
|
||||
@StartDatum DATE,
|
||||
@EndDatum DATE,
|
||||
@Tagespreis DECIMAL(10, 2)
|
||||
)
|
||||
RETURNS DECIMAL(10, 2)
|
||||
AS
|
||||
BEGIN
|
||||
DECLARE @AnzahlTage INT;
|
||||
DECLARE @Mietkosten DECIMAL(10, 2);
|
||||
|
||||
-- Berechne die Anzahl der Tage (mindestens 1 Tag)
|
||||
SET @AnzahlTage = DATEDIFF(DAY, @StartDatum, @EndDatum);
|
||||
|
||||
-- Falls das Enddatum vor oder gleich dem Startdatum ist, sind es mindestens 1 Tag
|
||||
IF @AnzahlTage < 1
|
||||
SET @AnzahlTage = 1;
|
||||
|
||||
-- Berechne die Gesamtkosten
|
||||
SET @Mietkosten = @AnzahlTage * @Tagespreis;
|
||||
|
||||
RETURN @Mietkosten;
|
||||
END;
|
||||
GO
|
||||
-- Table-Valued Function: fn_FahrzeugeAnStandort
|
||||
CREATE OR ALTER FUNCTION fn_FahrzeugeAnStandort (
|
||||
@StandortID INT
|
||||
)
|
||||
RETURNS TABLE
|
||||
AS
|
||||
RETURN (
|
||||
SELECT
|
||||
v.id,
|
||||
v.brand,
|
||||
v.model,
|
||||
v.licensePlate,
|
||||
v.buyDate,
|
||||
vc.description AS category,
|
||||
vc.pricePerDay,
|
||||
l.name AS locationName,
|
||||
l.address AS locationAddress
|
||||
FROM Vehicle v
|
||||
JOIN VehicleCategory vc ON v.vehicleCategoryID = vc.id
|
||||
JOIN Location l ON v.locationID = l.id
|
||||
WHERE v.locationID = @StandortID
|
||||
);
|
||||
Reference in New Issue
Block a user