Requêtes SQL complexes depuis un besoin métier

by fidji ChatGPT French ~288 tokens
Public excerpt Locked
Tu es un expert SQL. À partir du schéma et du besoin métier, écris la requête, explique chaque clause, propose les index utiles et une requête de contrôle…

About this prompt

Décris ton besoin en langage naturel et ton schéma de tables : le prompt produit une requête SQL optimisée, expliquée ligne par ligne, avec les index recommandés et une version de vérification sur un petit échantillon.

Example output

Provided by the author
Hypothèses : une commande est comptée à sa date de paiement ; les remboursements partiels ne sont pas déduits. ```sql WITH monthly AS ( SELECT c.name AS category, DATE_FORMAT(o.paid_at, '%Y-%m') AS month, SUM(oi.quantity * oi.unit_price) AS revenue FROM orders o JOIN order_items oi ON oi.order_id = o.id JOIN products p ON p.id = oi.product_id JOIN categories c ON c.id = p.category_id WHERE o.status <> 'refunded' AND o.paid_at >= DATE_SUB(CURDATE(), INTERVAL 12 MONTH) GROUP BY c.name, month ) SELECT category, month, revenue, ROUND(100 * (revenue - LAG(revenue) OVER w) / NULLIF(LAG(revenue) OVER w, 0), 1) AS evolution_pct FROM monthly WINDOW w AS (PARTITION BY category ORDER BY month); ``` Index : CREATE INDEX idx_orders_status_paid ON orders (status, paid_at);

Details

Model
ChatGPT
Prompt language
French
Who it's for
Développeurs, analystes et product managers qui écrivent du SQL occasionnellement
Length
~288 tokens (estimate)
Delivered as
Prompt text, shown online after purchase

Reviews

Buyers only

No reviews yet. Only buyers of this prompt can leave one.

Report this prompt