Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ```sql
- WITH AvgPrice AS (
- SELECT AVG(unitprice) AS avg_price
- FROM products
- ),
- SupplierOrders AS (
- SELECT s.supplierid, s.companyname, o.orderid, o.orderdate, od.productid, od.quantity, od.unitprice, o.shipregion, o.shipcountry
- FROM suppliers s
- JOIN products p ON s.supplierid = p.supplierid
- JOIN order_details od ON p.productid = od.productid
- JOIN orders o ON od.orderid = o.orderid
- ),
- CityOrderCount AS (
- SELECT supplierid, shipcity, COUNT(*) AS order_count
- FROM SupplierOrders
- GROUP BY supplierid, shipcity
- ),
- MaxCityOrder AS (
- SELECT supplierid, shipcity
- FROM (
- SELECT supplierid, shipcity, order_count,
- ROW_NUMBER() OVER (PARTITION BY supplierid ORDER BY order_count DESC) AS rn
- FROM CityOrderCount
- ) WHERE rn = 1
- ),
- OldestOrder AS (
- SELECT supplierid, orderid, orderdate
- FROM (
- SELECT supplierid, orderid, orderdate,
- ROW_NUMBER() OVER (PARTITION BY supplierid ORDER BY orderdate) AS rn
- FROM SupplierOrders
- ) WHERE rn = 1
- ),
- HighPriceProducts AS (
- SELECT DISTINCT s.supplierid, p.productname
- FROM suppliers s
- JOIN products p ON s.supplierid = p.supplierid
- CROSS JOIN AvgPrice
- WHERE p.unitprice > AvgPrice.avg_price
- ),
- DistinctRegions AS (
- SELECT supplierid, COUNT(DISTINCT shipregion) AS region_count
- FROM SupplierOrders
- GROUP BY supplierid
- ),
- CategorySales AS (
- SELECT c.categoryid, c.categoryname, od.productid, SUM(od.quantity * od.unitprice) AS total_sales
- FROM categories c
- JOIN products p ON c.categoryid = p.categoryid
- JOIN order_details od ON p.productid = od.productid
- JOIN orders o ON od.orderid = o.orderid
- GROUP BY c.categoryid, c.categoryname, od.productid
- ),
- TopCategory AS (
- SELECT categoryid, categoryname
- FROM (
- SELECT categoryid, categoryname,
- ROW_NUMBER() OVER (ORDER BY total_sales DESC) AS rn
- FROM CategorySales
- ) WHERE rn = 1
- ),
- TopProductInCategory AS (
- SELECT cs.categoryid, cs.productid, p.productname
- FROM CategorySales cs
- JOIN products p ON cs.productid = p.productid
- JOIN TopCategory tc ON cs.categoryid = tc.categoryid
- WHERE cs.total_sales = (
- SELECT MAX(total_sales)
- FROM CategorySales
- WHERE categoryid = tc.categoryid
- )
- )
- SELECT s.supplierid, s.companyname,
- mco.shipcity AS city_with_most_orders,
- oo.orderid AS oldest_order_id, oo.orderdate AS oldest_order_date,
- LISTAGG(hpp.productname, ', ') WITHIN GROUP (ORDER BY hpp.productname) AS high_price_products,
- CASE WHEN dr.region_count >= 10 THEN 'Da' ELSE 'Nu' END AS delivered_to_10_regions,
- tc.categoryname AS top_category_name, tp.productname AS top_product_in_category
- FROM suppliers s
- LEFT JOIN MaxCityOrder mco ON s.supplierid = mco.supplierid
- LEFT JOIN OldestOrder oo ON s.supplierid = oo.supplierid
- LEFT JOIN HighPriceProducts hpp ON s.supplierid = hpp.supplierid
- LEFT JOIN DistinctRegions dr ON s.supplierid = dr.supplierid
- LEFT JOIN TopCategory tc ON 1 = 1
- LEFT JOIN TopProductInCategory tp ON tc.categoryid = tp.categoryid
- GROUP BY s.supplierid, s.companyname, mco.shipcity, oo.orderid, oo.orderdate, dr.region_count, tc.categoryname, tp.productname;
- ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement