Aller au contenu

Le suffixe _IF peut être ajouté au nom de n’importe quelle fonction d’agrégation. Dans ce cas, la fonction d’agrégation accepte un argument supplémentaire : une condition.

func.min_if(<column>, <cond>)
func.min_if(table.budget, table.departing=='IT').alias('min_it_budget')
| min_it_budget |
|---------------|
| 2000 |
MIN_IF(<column>, <cond>)

Créer une table et insérer des données d’exemple

CREATE TABLE project_budgets (
id INT,
project_id INT,
department VARCHAR,
budget FLOAT
);
INSERT INTO project_budgets (id, project_id, department, budget)
VALUES (1, 1, 'HR', 1000),
(2, 1, 'IT', 2000),
(3, 1, 'Marketing', 3000),
(4, 2, 'HR', 1500),
(5, 2, 'IT', 2500);

Exemple de requête : trouver le budget minimal du service informatique

SELECT MIN_IF(budget, department = 'IT') AS min_it_budget
FROM project_budgets;

Résultat

| min_it_budget |
|---------------|
| 2000 |