You are on page 1of 7

BOUTIQUE MANAGEMENT SYSTEM

ASSIGNMENT 4
-- procedures
--1
CREATE PROCEDURE GetCustomerInfo
@customerID INT
AS
BEGIN
SELECT *
FROM Customer
WHERE CustomerID = @customerID;
END;

EXEC GetCustomerInfo @customerID =1222;

--2
CREATE PROCEDURE UpdateCustomerInfo
@customerID INT,
@newName VARCHAR(255),
@newContactNumber VARCHAR(15),
@newAddress VARCHAR(255)
AS
BEGIN
UPDATE Customer
SET Name = @newName, ContactNumber = @newContactNumber, Address = @newAddress
WHERE CustomerID = @customerID;
END;
EXEC UpdateCustomerInfo
@customerID = 1222,
@newName = 'John Marshalls',
@newContactNumber = '9876543210',
@newAddress = '456 Oak St';

--3
CREATE PROCEDURE GetEmployeeInfo
@employeeID INT
AS
BEGIN
SELECT *
FROM Employee
WHERE EmployeeID = @employeeID;
END;
EXEC GetEmployeeInfo @employeeID = 1720;

--4
CREATE PROCEDURE AddCustomer
@customerID int,
@name VARCHAR(255),
@gender VARCHAR(10),
@dateOfBirth DATE,
@contactNumber VARCHAR(15),
@email VARCHAR(100),
@registrationDate DATE,
@purchaseHistory DATE,
@address VARCHAR(255),
@customerStatus VARCHAR(255)
AS
BEGIN
INSERT INTO Customer ( CustomerID,Name, Gender, DateOfBirth, ContactNumber, Email,
RegistrationDate, PurchaseHistory, Address, CustomerStatus)
VALUES ( @customerID ,@name, @gender, @dateOfBirth, @contactNumber, @email,
@registrationDate, @purchaseHistory, @address, @customerStatus);
END;
EXEC AddCustomer
@customerID = '1001',
@name = 'John Doe',
@gender = 'Male',
@dateOfBirth = '1990-01-01',
@contactNumber = '1234567890',
@email = 'john.doe@example.com',
@registrationDate = '2023-01-01',
@purchaseHistory = '2022-12-01',
@address = '123 Main St',
@customerStatus = 'walk-in';

--5
CREATE PROCEDURE GetTotalSalesTrend
@StartYear INT,
@EndYear INT
AS
BEGIN
SELECT YEAR(SaleDate) AS Year, SUM(sale_price * Quantity) AS TotalSales
FROM Sales
WHERE YEAR(SaleDate) BETWEEN @StartYear AND @EndYear
GROUP BY YEAR(SaleDate);
END;
EXEC GetTotalSalesTrend @StartYear = 2022, @EndYear = 2023;

--6
CREATE PROCEDURE GetTopSellingProducts
@TopN INT
AS
BEGIN
SELECT TOP (@TopN) P.name, SUM(S.Quantity) AS TotalQuantitySold
FROM Products P
JOIN Sales S ON P.productId = S.ProductID
GROUP BY P.name
ORDER BY TotalQuantitySold DESC;
END;

EXEC GetTopSellingProducts @TopN = 10;

--7
CREATE PROCEDURE GetTotalPurchaseBySupplier
@Supplier VARCHAR(255)
AS
BEGIN
SELECT p.supplier, SUM(p.purchase_price) AS TotalPurchaseAmount
FROM purchase p
WHERE p.supplier = @Supplier
GROUP BY p.supplier;
END;

EXEC GetTotalPurchaseBySupplier @Supplier = 'Sapphire';


--8
CREATE PROCEDURE GetTotalSalesByGender
@Gender VARCHAR(10)
AS
BEGIN
SELECT c.Gender, SUM(s.sale_price*s.Quantity) AS TotalSales
FROM Customer c
JOIN Sales s ON c.CustomerID = s.CustomerID
WHERE c.Gender = @Gender
GROUP BY c.Gender;
END;

EXEC GetTotalSalesByGender @Gender = 'Male';

--9
CREATE PROCEDURE GetEmployeeSalaryDistribution
@Position VARCHAR(50)
AS
BEGIN
SELECT Position, AVG(Salary) AS AvgSalary
FROM Employee
WHERE Position = @Position
GROUP BY Position;
END;

EXEC GetEmployeeSalaryDistribution @Position = 'Manager';

--10
CREATE PROCEDURE GetTotalSalesByCategory
AS
BEGIN
SELECT p.Category, SUM(s.sale_price * s.Quantity) AS TotalSales
FROM Products p
JOIN Sales s ON p.productId = s.ProductID
GROUP BY p.Category;
END;

EXEC GetTotalSalesByCategory;

--11
CREATE PROCEDURE GetTotalSalesByCustomer
AS
BEGIN
SELECT c.CustomerID, c.Name, SUM(s.sale_price * s.Quantity) AS TotalSales
FROM Customer c
LEFT JOIN Sales s ON c.CustomerID = s.CustomerID
GROUP BY c.CustomerID, c.Name;
END;

EXEC GetTotalSalesByCustomer;

--12
CREATE PROCEDURE GetTotalSalesByDateRange
@StartDate DATE,
@EndDate DATE
AS
BEGIN
SELECT SaleDate, SUM(sale_price * Quantity) AS TotalSales
FROM Sales
WHERE SaleDate BETWEEN @StartDate AND @EndDate
GROUP BY SaleDate;
END;

EXEC GetTotalSalesByDateRange @StartDate = '2023-01-01', @EndDate = '2023-12-31';

--13
CREATE PROCEDURE GetTotalPurchaseAmountBySupplier
AS
BEGIN
SELECT supplier, SUM(purchase_price) AS TotalPurchaseAmount
FROM purchase
GROUP BY supplier;
END;

EXEC GetTotalPurchaseAmountBySupplier;

--14
CREATE PROCEDURE InsertPurchase
@ProductId INT,
@PurchaseDate DATE,
@PurchasePrice MONEY,
@Quantity INT,
@Supplier VARCHAR(255)
AS
BEGIN
DECLARE @PurchaseId INT

-- Generate a unique PurchaseID


SELECT @PurchaseId = ISNULL(MAX(purchaseID), 0) + 1 FROM purchase

-- Insert the new purchase record


INSERT INTO purchase (purchaseID, productId, date_of_purchase, purchase_price,
quantity, supplier)
VALUES (@PurchaseId, @ProductId, @PurchaseDate, @PurchasePrice, @Quantity,
@Supplier)
END;

EXEC InsertPurchase
@ProductId = 6115,
@PurchaseDate = '2023-12-15',
@PurchasePrice = 2000.00,
@Quantity = 10,
@Supplier = 'J.';

--15
CREATE PROCEDURE GetTotalRevenueByCategory
@Category VARCHAR(50),
@StartDate DATE,
@EndDate DATE
AS
BEGIN
SELECT
P.category,
SUM(S.sale_price * s.Quantity) AS TotalRevenue
FROM
Sales S
JOIN Products P ON S.ProductID = P.ProductID
WHERE
P.category = @Category
AND S.SaleDate BETWEEN @StartDate AND @EndDate
GROUP BY
P.category;
END;

EXEC GetTotalRevenueByCategory
@Category = 'Clothing',
@StartDate = '2023-01-01',
@EndDate = '2023-12-31';

--viewss
--1
CREATE VIEW ProductInventory AS
SELECT
P.ProductID,
P.name AS ProductName,
P.size,
P.color,
ISNULL(SUM(S.Quantity), 0) AS TotalSold
FROM
Products P
LEFT JOIN
Sales S ON P.ProductID = S.ProductID
GROUP BY
P.ProductID, P.name, P.size, P.color;

SELECT
ProductID,
ProductName,
size,
color,
TotalSold
FROM
ProductInventory;

--2
CREATE VIEW SalesByProductCategory AS
SELECT
P.category,
COUNT(S.SaleID) AS TotalSales,
SUM(S.sale_price * s.Quantity) AS Revenue
FROM
Sales S
JOIN
Products P ON S.ProductID = P.ProductID
GROUP BY
P.category;

SELECT
category,
TotalSales,
Revenue
FROM
SalesByProductCategory;

--3
CREATE VIEW EmployeeSalarySummary AS
SELECT
E.EmployeeID,
E.Name AS EmployeeName,
E.Position,
E.Salary
FROM
Employee E;

SELECT
EmployeeID,
EmployeeName,
Position,
Salary
FROM
EmployeeSalarySummary;

You might also like