Extraits de code
Quelques requêtes clés
CREATE TABLE commande (
id_commande INTEGER PRIMARY KEY AUTOINCREMENT,
date_commande DATE NOT NULL,
etat TEXT NOT NULL CHECK(etat IN ('en_cours', 'livree', 'annulee')),
montant_total DECIMAL(10, 2) CHECK(montant_total >= 0),
id_client INTEGER NOT NULL,
id_commercial INTEGER NOT NULL,
FOREIGN KEY(id_client) REFERENCES client(id_client),
FOREIGN KEY(id_commercial) REFERENCES commercial(id_commercial)
);
-- Top 3 des produits les plus vendus toutes commandes confondues
SELECT
p.libelle,
SUM(lc.quantite) AS total_vendu
FROM ligne_commande lc
JOIN produit p ON p.id_produit = lc.id_produit
JOIN commande c ON c.id_commande = lc.id_commande
WHERE c.etat <> 'annulee'
GROUP BY p.libelle
ORDER BY total_vendu DESC
LIMIT 3;
-- Produits sous le seuil d'alerte, par magasin
SELECT
m.nom AS magasin,
p.libelle AS produit,
s.quantite,
p.seuil_alerte
FROM stock s
JOIN produit p ON p.id_produit = s.id_produit
JOIN magasin m ON m.id_magasin = s.id_magasin
WHERE s.quantite < p.seuil_alerte
ORDER BY m.nom, p.libelle;