Advertisement
mazursandu

Untitled

Dec 2nd, 2024
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 3.26 KB | None | 0 0
  1. ```sql
  2. WITH AvgPrice AS (
  3.    SELECT AVG(unitprice) AS avg_price
  4.    FROM products
  5. ),
  6. SupplierOrders AS (
  7.    SELECT s.supplierid, s.companyname, o.orderid, o.orderdate, od.productid, od.quantity, od.unitprice, o.shipregion, o.shipcountry
  8.    FROM suppliers s
  9.    JOIN products p ON s.supplierid = p.supplierid
  10.    JOIN order_details od ON p.productid = od.productid
  11.    JOIN orders o ON od.orderid = o.orderid
  12. ),
  13. CityOrderCount AS (
  14.    SELECT supplierid, shipcity, COUNT(*) AS order_count
  15.    FROM SupplierOrders
  16.    GROUP BY supplierid, shipcity
  17. ),
  18. MaxCityOrder AS (
  19.    SELECT supplierid, shipcity
  20.    FROM (
  21.        SELECT supplierid, shipcity, order_count,
  22.               ROW_NUMBER() OVER (PARTITION BY supplierid ORDER BY order_count DESC) AS rn
  23.        FROM CityOrderCount
  24.    ) WHERE rn = 1
  25. ),
  26. OldestOrder AS (
  27.    SELECT supplierid, orderid, orderdate
  28.    FROM (
  29.        SELECT supplierid, orderid, orderdate,
  30.               ROW_NUMBER() OVER (PARTITION BY supplierid ORDER BY orderdate) AS rn
  31.        FROM SupplierOrders
  32.    ) WHERE rn = 1
  33. ),
  34. HighPriceProducts AS (
  35.    SELECT DISTINCT s.supplierid, p.productname
  36.    FROM suppliers s
  37.    JOIN products p ON s.supplierid = p.supplierid
  38.    CROSS JOIN AvgPrice
  39.    WHERE p.unitprice > AvgPrice.avg_price
  40. ),
  41. DistinctRegions AS (
  42.    SELECT supplierid, COUNT(DISTINCT shipregion) AS region_count
  43.    FROM SupplierOrders
  44.    GROUP BY supplierid
  45. ),
  46. CategorySales AS (
  47.    SELECT c.categoryid, c.categoryname, od.productid, SUM(od.quantity * od.unitprice) AS total_sales
  48.    FROM categories c
  49.    JOIN products p ON c.categoryid = p.categoryid
  50.    JOIN order_details od ON p.productid = od.productid
  51.    JOIN orders o ON od.orderid = o.orderid
  52.    GROUP BY c.categoryid, c.categoryname, od.productid
  53. ),
  54. TopCategory AS (
  55.    SELECT categoryid, categoryname
  56.    FROM (
  57.        SELECT categoryid, categoryname,
  58.               ROW_NUMBER() OVER (ORDER BY total_sales DESC) AS rn
  59.        FROM CategorySales
  60.    ) WHERE rn = 1
  61. ),
  62. TopProductInCategory AS (
  63.    SELECT cs.categoryid, cs.productid, p.productname
  64.    FROM CategorySales cs
  65.    JOIN products p ON cs.productid = p.productid
  66.    JOIN TopCategory tc ON cs.categoryid = tc.categoryid
  67.    WHERE cs.total_sales = (
  68.        SELECT MAX(total_sales)
  69.        FROM CategorySales
  70.        WHERE categoryid = tc.categoryid
  71.    )
  72. )
  73. SELECT s.supplierid, s.companyname,
  74.       mco.shipcity AS city_with_most_orders,
  75.       oo.orderid AS oldest_order_id, oo.orderdate AS oldest_order_date,
  76.       LISTAGG(hpp.productname, ', ') WITHIN GROUP (ORDER BY hpp.productname) AS high_price_products,
  77.       CASE WHEN dr.region_count >= 10 THEN 'Da' ELSE 'Nu' END AS delivered_to_10_regions,
  78.       tc.categoryname AS top_category_name, tp.productname AS top_product_in_category
  79. FROM suppliers s
  80. LEFT JOIN MaxCityOrder mco ON s.supplierid = mco.supplierid
  81. LEFT JOIN OldestOrder oo ON s.supplierid = oo.supplierid
  82. LEFT JOIN HighPriceProducts hpp ON s.supplierid = hpp.supplierid
  83. LEFT JOIN DistinctRegions dr ON s.supplierid = dr.supplierid
  84. LEFT JOIN TopCategory tc ON 1 = 1
  85. LEFT JOIN TopProductInCategory tp ON tc.categoryid = tp.categoryid
  86. GROUP BY s.supplierid, s.companyname, mco.shipcity, oo.orderid, oo.orderdate, dr.region_count, tc.categoryname, tp.productname;
  87. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement