'SELECT sum but with where in MySQL
I'm trying sum up the total_grand column of my orders table.
So I have this query
SELECT sum(total_grand) as total
FROM `orders`
WHERE overall_status in ("In-Transit","Not Yet Shipped","Not Yet Validated");
It works fine. But I need to add new conditional statement.
Here's my sample orders table columns with data.
| total_grand | overall_status | televalidator_user_id |
|---|---|---|
| 100 | In-Transit | 1 |
| 200 | Not Yet Shipped | 1 |
| 300 | Not Yet Validated | NULL |
| 400 | Not Yet Validated | 1 |
| 500 | In-Transit | 1 |
| --------------------- | ------------------ | ------------------ |
I'm trying to sum up the total_grand but if televalidator_user_id is NULL, It should not be added to the sump
Since there is null televalidator_user_id on row 3
the output must be: 1200
Solution 1:[1]
SELECT sum(total_grand) as total
FROM `orders`
WHERE overall_status in ("In-Transit", "Not Yet Shipped", "Not Yet Validated")
AND televalidator_user_id is not null;
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | dgnk |
