Hello,
I've tested it and it doesn't always work. I've attached the different codes. Thank you
Here are my codes :
-- Création de la table Customers
USE WAREHOUSE PURVIEW_WH;
CREATE DATABASE IF NOT EXISTS PURVIEW_TEST;
CREATE SCHEMA IF NOT EXISTS PV;
--DROP DATABASE PURVIEW_TEST;
-- Création de la table Customers
CREATE TABLE Customers (
CustomerID INT,
CustomerName VARCHAR(50),
Email VARCHAR(100),
PRIMARY KEY (CustomerID)
);
-- Création de la table Orders
CREATE TABLE Orders (
OrderID INT,
CustomerID INT,
OrderDate DATE,
PRIMARY KEY (OrderID),
FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID)
);
-- Création de la table Products
CREATE TABLE Products (
ProductID INT,
ProductName VARCHAR(50),
Price DECIMAL(10, 2),
PRIMARY KEY (ProductID)
);
-- Insertion de quelques données
INSERT INTO Customers VALUES (1, 'John Doe', 'john.doe@email.com');
INSERT INTO Customers VALUES (2, 'Jane Smith', 'jane.smith@email.com');
INSERT INTO Orders VALUES (101, 1, '2024-01-01');
INSERT INTO Orders VALUES (102, 1, '2024-01-02');
INSERT INTO Orders VALUES (103, 2, '2024-01-03');
INSERT INTO Products VALUES (1, 'Product A', 50.00);
INSERT INTO Products VALUES (2, 'Product B', 75.00);
-- Création de la vue pour afficher les détails de la commande
CREATE VIEW OrderDetails AS
SELECT
o.OrderID,
o.OrderDate,
c.CustomerName,
p.ProductName,
p.Price
FROM
Orders o
JOIN
Customers c ON o.CustomerID = c.CustomerID
JOIN
Products p ON o.OrderID = p.ProductID;
-- Création de la vue pour obtenir un résumé des commandes par client
CREATE VIEW CustomerOrderSummary AS
SELECT
c.CustomerID,
c.CustomerName,
COUNT(o.OrderID) AS TotalOrders,
SUM(p.Price) AS TotalSpent
FROM
Customers c
LEFT JOIN
Orders o ON c.CustomerID = o.CustomerID
LEFT JOIN
Products p ON o.OrderID = p.ProductID
GROUP BY
c.CustomerID, c.CustomerName;
-- Création de la stored procedure pour obtenir des informations agrégées en fonction d'un critère de date
CREATE OR REPLACE PROCEDURE GetOrderSummaryByDate(targetDate DATE)
RETURNS TABLE (CustomerName VARCHAR, TotalOrders INT, TotalSpent DECIMAL)
LANGUAGE SQL
EXECUTE AS CALLER
AS
'
SELECT
o.CustomerName,
COUNT(o.OrderID) AS TotalOrders,
SUM(p.Price) AS TotalSpent
FROM
OrderDetails o
WHERE
o.OrderDate = targetDate
GROUP BY
o.CustomerName;
'
-- Exemple d'appel de la stored procedure
CALL GetOrderSummaryByDate(2024-01-02);