PROJET DE SQL
1-Écrivez une requête pour récupérer les noms des clients qui ont commandé à la
fois des "Bonbons" et des "Biscuits", ainsi que le coût total de ces produits
commandés par chaque client. Le coût doit être calculé en multipliant la quantité
par le prix
SELECT
client.id_client,
client.nom_client,
produit.nom_produit,
SUM([Link] * dc.prix_unitaire) AS total
FROM Clients c
JOIN Commandes cmd ON c.id_client = cmd.id_client
JOIN Details_Commande dc ON cmd.id_commande = dc.id_commande
JOIN Produits p ON dc.id_produit = p.id_produit
GROUP BY c.id_client, c.nom_client, p.nom_produit;
2- Écrivez une requête pour récupérer les noms des clients qui ont
commandé des "Bonbons", ainsi que le coût total des bonbons
commandés par chaque client.
SELECT
cl.nom_client, SUM([Link] * lc.prix_unitaire) AS total_bonbons
FROM DetailCommande dc
JOIN Commandes c ON dc.id_commande = c.id_commande
JOIN Clients cl ON c.id_client = cl.id_client
JOIN Produits p ON dc.id_produit = p.id_produit
WHERE p.nom_produit = 'Bonbons'
GROUP BY cl.nom_client;
3-Écrivez une requête pour récupérer le nombre total de "biscuits" et de
"bonbons" commandés par chaque client, ainsi que le coût total de ces
commandes.
SELECT
cl.nom_client, SUM([Link] * lc.prix_unitaire) AS total_glaces
FROM DetailCommande dc
JOIN Commandes c ON dc.id_commande = c.id_commande
JOIN Clients cl ON c.id_client = cl.id_client
JOIN Produits p ON dc.id_produit = p.id_produit
WHERE p.nom_produit = 'glaces'
GROUP BY cl.nom_client;
4-Écrivez une requête pour récupérer le nombre total de "biscuits" et de
"bonbons" commandés par chaque client, ainsi que le coût total de ces
commandes.
SELECT
c.nom_client,
SUM([Link]) AS total_quantite,
SUM([Link] * [Link]) AS total
FROM commandes cmd
JOIN clients c ON cmd.client_id = c.client_id
JOIN produits p ON cmd.produit_id = p.produit_id
WHERE p.nom_produit IN ('Bonbons', 'Biscuits')
GROUP BY c.nom_client;
5- Écrivez une requête pour récupérer les noms des clients qui ont passé
le plus grand nombre de commandes, ainsi que le nombre total de
commandes passées par chaque client.
WITH TotalCommandes AS (
SELECT c.id_client, cl.nom_client, COUNT(c.id_commande) AS
nombre_commandes
FROM Commandes c
JOIN Clients cl ON c.id_client = cl.id_client
GROUP BY c.id_client, cl.nom_client
)
SELECT nom_client, nombre_commandes
FROM TotalCommandes
WHERE nombre_commandes = (SELECT MAX(nombre_commandes) FROM
TotalCommandes);
6-Écrivez une requête pour récupérer les noms des produits les plus
commandés, ainsi que la quantité totale de chaque produit commandé.
WITH Q_p_produit AS (
SELECT p.nom_produit, SUM([Link]) AS total_quantite
FROM commandes o
JOIN produits p ON o.produit_id = p.produit_id
GROUP BY p.nom_produit
)
SELECT nom_produit, total_quantite
FROM Q_p_produit
WHERE total_quantite = (SELECT MAX(total_quantite) FROM Q_p_produit);
7- Écrivez une requête pour récupérer les noms des clients qui ont passé
le plus grand nombre de commandes, ainsi que le nombre total de
commandes passées par chaque client.
WITH CmdParJour AS (
SELECT cmd.id_client, cl.nom_client,
EXTRACT(DOW FROM cmd.date_commande) AS jour_semaine,
COUNT(*) AS nbr_commandes
FROM Commandes cmd
JOIN Clients cl ON cmd.id_client = cl.id_client
GROUP BY cmd.id_client, cl.nom_client, EXTRACT(DOW FROM cmd.date_commande)
),
ClientsJours AS (
SELECT id_client, nom_client, SUM(nbr_commandes) AS total_commandes
FROM CmdParJour
GROUP BY id_client, nom_client
HAVING COUNT(DISTINCT jour_semaine) = 7
)
SELECT nom_client, total_commandes
FROM ClientsJours;