| 6 | | |
| | 6 | with latestPrice as ( |
| | 7 | select p.art_id,p.price as latest_price |
| | 8 | from price p |
| | 9 | where p.price_eff_date = ( |
| | 10 | select max(price_eff_date) |
| | 11 | from price |
| | 12 | where p.art_id=p.art_id |
| | 13 | ) |
| | 14 | ) |
| | 15 | select c.cust_company_name as customerName, |
| | 16 | c.user_id as customerId, |
| | 17 | a.art_name as articleName, |
| | 18 | count(au.unit_id) as quantity, |
| | 19 | lp.latest_price as unitPrice, |
| | 20 | count(au.unit_id)*lp.latest_price as total_spent, |
| | 21 | sum(o.ord_sum) as orderTotal |
| | 22 | from orders o |
| | 23 | join article_unit au on o.ord_id = au.ord_id |
| | 24 | join article a on au.art_id = a.art_id |
| | 25 | join customer c on o.cust_id = c.user_id |
| | 26 | join latestPrice lp on a.art_id=lp.art_id |
| | 27 | where o.ord_date between '2024-01-01' and '2024-12-31' |
| | 28 | group by c.cust_company_name,c.user_id,a.art_name,lp.latest_price |
| | 29 | order by c.cust_company_name,a.art_name; |
| 18 | | |
| | 47 | with salesByPrice as (select au.art_id, |
| | 48 | p.price, |
| | 49 | p.price_eff_date, |
| | 50 | count(au.unit_id) as unitsSold, |
| | 51 | count(au.unit_id) * p.price as revenue |
| | 52 | from article_unit au |
| | 53 | join orders o on au.ord_id = o.ord_id |
| | 54 | join price p on au.art_id = p.art_id |
| | 55 | where o.ord_date >= p.price_eff_date |
| | 56 | and o.ord_date < coalesce( |
| | 57 | (select min(price_eff_date) |
| | 58 | from price p2 |
| | 59 | where p2.art_id = p.art_id |
| | 60 | and p2.price_eff_date > p.price_eff_date), |
| | 61 | '9999-12-31' |
| | 62 | ) |
| | 63 | group by au.art_id, p.price, p.price_eff_date) |
| | 64 | select a.art_name as articleName, |
| | 65 | sp.price as unitPrice, |
| | 66 | sp.price_eff_date as priceEffectiveDate, |
| | 67 | sp.unitsSold, |
| | 68 | sp.revenue |
| | 69 | from salesByPrice sp |
| | 70 | join article a on sp.art_id = a.art_id |
| | 71 | order by a.art_name,sp.price_eff_date; |
| 24 | | |
| | 77 | with ArticleSales as ( |
| | 78 | select au.art_id, |
| | 79 | a.ctg_id, |
| | 80 | w.wh_id, |
| | 81 | c.city_id, |
| | 82 | r.region_id, |
| | 83 | p.price as unitPrice, |
| | 84 | count(au.unit_id) as unitsSold, |
| | 85 | count(au.unit_id)*p.price as income |
| | 86 | from article_unit au |
| | 87 | join article a on au.art_id = a.art_id |
| | 88 | join warehouse w on au.wh_id = w.wh_id |
| | 89 | join city c on w.city_id = c.city_id |
| | 90 | join region r on c.region_id = r.region_id |
| | 91 | join price p on a.art_id = p.art_id |
| | 92 | join orders o on au.ord_id = o.ord_id |
| | 93 | where o.ord_date>=p.price_eff_date |
| | 94 | group by au.art_id, a.ctg_id, w.wh_id, c.city_id, r.region_id, p.price, p.price |
| | 95 | ) |
| | 96 | select r.region_name as region, |
| | 97 | cat.ctg_name as category, |
| | 98 | sum(asales.unitsSold) as totalUnitsSold, |
| | 99 | sum(asales.income) as totalIncome |
| | 100 | from ArticleSales asales |
| | 101 | join category cat on asales.ctg_id=cat.ctg_id |
| | 102 | join region r on asales.region_id=r.region_id |
| | 103 | group by r.region_name, cat.ctg_name |
| | 104 | order by r.region_name, totalIncome desc; |
| 30 | | |
| | 110 | select w.wh_id as warehouseId, |
| | 111 | w.wh_adr as warehouseAddress, |
| | 112 | c.city_name as warehouseCity, |
| | 113 | r.region_name as warehouseRegion, |
| | 114 | extract(year from o.ord_date) as year, |
| | 115 | extract(quarter from o.ord_date) as quarter, |
| | 116 | count(au.unit_id) as totalArticlesSold, |
| | 117 | sum(o.ord_sum) as totalSales |
| | 118 | from warehouse w |
| | 119 | join city c on w.city_id = c.city_id |
| | 120 | join region r on c.region_id = r.region_id |
| | 121 | join article_unit au on w.wh_id = au.wh_id |
| | 122 | join orders o on au.ord_id = o.ord_id |
| | 123 | group by w.wh_id, w.wh_adr, c.city_name, r.region_name, extract(year from o.ord_date), extract(quarter from o.ord_date) |
| | 124 | order by w.wh_id,year,quarter; |
| 36 | | |
| | 130 | select w.wh_id as warehouseId, |
| | 131 | c.city_name as warehouseCity, |
| | 132 | r.region_name as warehouseRegion, |
| | 133 | extract(year from o.ord_date) as year, |
| | 134 | sum(o.ord_sum) as totalIncome, |
| | 135 | sum(ord_sum)-sum(au.unit_cost_price) as total_profit |
| | 136 | from warehouse w |
| | 137 | join city c on w.city_id = c.city_id |
| | 138 | join region r on c.region_id = r.region_id |
| | 139 | join article_unit au on w.wh_id = au.wh_id |
| | 140 | join orders o on au.ord_id = o.ord_id |
| | 141 | group by w.wh_id, c.city_name, r.region_name, o.ord_date |
| | 142 | order by w.wh_id,year; |