Alexander_89

Test_Sportmaster

Apr 30th, 2023
765
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PL/SQL 1.04 KB | None | 0 0
  1. /*create table tab(
  2.   week_end VARCHAR(30),
  3.   category VARCHAR(30),
  4.   out_of_stock FLOAT,
  5.   delivery_lag FLOAT,
  6.   product_deficit FLOAT,
  7.   nostock FLOAT,
  8.   other FLOAT);
  9. INSERT INTO tab VALUES ('23.01.2022', 'Equipment', 0.24, 0.02, 0.04, 0.07, 0.11),
  10. ('23.01.2022', 'Shoes', 0.14, 0.04, 0.04, 0.03, 0.03),
  11. ('23.01.2022', 'Clothing', 0.16, 0.03, 0.05, 0.06, 0.02);*/
  12. WITH t3 AS (
  13.     SELECT t1.week_end, t1.category, t1.out_of_stock, t2.*
  14.     FROM tab AS t1
  15.         cross join lateral (
  16.              VALUES
  17.              (t1.delivery_lag, 'delivery_lag'),
  18.              (t1.product_deficit, 'product_deficit'),
  19.              (t1.nostock, 'nostock'),
  20.              (t1.other, 'other')
  21.         ) AS t2(d, cause1)
  22. ),
  23. t4 AS (
  24.     SELECT *, DENSE_RANK() OVER(PARTITION BY category ORDER BY d DESC) AS r
  25.     FROM t3
  26. ),
  27. t5 AS (
  28.     SELECT *
  29.     FROM t4
  30.     WHERE r=1
  31. ),
  32. t6 AS (
  33.   SELECT week_end, category, out_of_stock, cause1||' ('||d||')' AS cause2
  34.   FROM t5
  35. )
  36. SELECT week_end, category, out_of_stock, STRING_AGG(cause2, ', ') AS cause
  37. FROM t6
  38. GROUP BY week_end, category, out_of_stock
Advertisement
Add Comment
Please, Sign In to add comment