148 | | |
| 148 | with ProductSales as (select au.art_id, |
| 149 | a.art_name, |
| 150 | cat.ctg_name as category, |
| 151 | sum(p.price) as totalIncome, |
| 152 | sum(au.unit_cost_price) as totalCost, |
| 153 | sum(p.price) - sum(au.unit_cost_price) as totalProfit, |
| 154 | count(au.unit_id) as totalUnitsSold |
| 155 | from article_unit au |
| 156 | join article a on au.art_id = a.art_id |
| 157 | join category cat on a.ctg_id = cat.ctg_id |
| 158 | join price p on a.art_id = p.art_id |
| 159 | join orders o on au.ord_id = o.ord_id |
| 160 | where o.ord_date between '2024-01-01' and '2024-12-31' |
| 161 | and o.ord_date >= p.price_eff_date |
| 162 | group by au.art_id, a.art_name, cat.ctg_name) |
| 163 | select art_id, |
| 164 | art_name, |
| 165 | category, |
| 166 | totalUnitsSold, |
| 167 | totalIncome, |
| 168 | totalCost, |
| 169 | totalProfit |
| 170 | from ProductSales |
| 171 | order by totalProfit desc; |