I have 2 tables, 'manufacturers' and 'products'. A manufacturer can have multiple products and a product has a 'qty', 'price' and 'cost' field.
Manufacturers table
| manufacturer_id | name |
|---|---|
| 1 | ABC |
| 2 | Meteor |
| 3 | XYZ |
| 4 | Airfix |
Products table
| product_id | manufacturer_id | price | cost | qty |
|---|---|---|---|---|
| 100 | 1 | 123.32 | 32.23 | 32 |
| 101 | 2 | 123.32 | 32.23 | 23 |
| 102 | 3 | 123.32 | 32.23 | 16 |
| 103 | 2 | 123.32 | 32.23 | 8 |
| 104 | 1 | 123.32 | 12.45 | 5 |
| 105 | 3 | 123.32 | 0.00 | 3 |
| 106 | 3 | 123.32 | 32.23 | 99 |
| 107 | 4 | 123.32 | 32.23 | 88 |
I've LEFT JOINed them together and can get SUMs on the 'price' and 'cost' fields based on multiplying by 'qty' but I'd like to get a 'cost_complete' flag set to false if ANY of the product.cost fields from a specific manufacturer are not set, thus allowing me to identify any manufacturer who has products that don't yet have a 'cost' set.
This is what I have so far, but I think that my 'cost_complete' flag gets overwritten for each product.
SELECT m.name, p.quantity, p.price, p.cost,
SUM(p.quantity) AS total_quantity,
SUM(p.price*p.quantity) AS total_price,
SUM(p.cost*p.quantity) AS total_cost, IF(p.cost>0, 'true','false') AS cost_complete
FROM manufacturer m LEFT JOIN product p ON m.manufacturer_id = p.manufacturer_id GROUP BY m.manufacturer_id ORDER by m.name
Modified to explain what I am expecting:
| name | quantity | total_price | total_cost | cost_complete |
|---|---|---|---|---|
| ABC | 56 | £345.23 | £123.32 | true |
| Meteor | 12 | £1345.23 | £23.32 | true |
| XYZ | 123 | £345.23 | £65.12 | false |
| Airfix | 66 | £445.23 | £12.12 | true |
...where the cost_complete column is set to false when not all of the cost fields have a value greater than 0.
You can do a conditional sum in case the p.cost is null (or is lower than 0 as you have in your code). Then you will have the number of products that don´t have a p.cost (if it is 0, all products have a cost)
P.D. To answer your comment, you can do it this way: