Advertisement
Pandaaaa906

Untitled

Sep 1st, 2022 (edited)
1,437
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. -- 成本异常
  3. with tmp as (
  4. select
  5. id,
  6. brand_name 品牌,
  7. cat_no 货号,
  8. package_value 规格,
  9. normalize_package(package_value) mg,
  10. round("cost",2) 成本,
  11. price 市场价,
  12. price_expiry_date 价格有效期,
  13. lag(normalize_package(package_value), 1) OVER(w) 上一个mg,
  14. lag(cost, 1) OVER(w) 上一个成本,
  15. lead(normalize_package(package_value), 1) OVER(w) 下一个mg,
  16. lead(cost, 1) OVER(w) 下一个成本,
  17. case when lag(cost, 1) OVER(w) > cost then 1
  18. else 0 end
  19. 成本异常
  20.  
  21. FROM t_product_package pkg
  22.  
  23. where pkg.brand_name ilike 'cato'
  24. and cost is not null
  25. and is_delete = 1
  26. and price_expiry_date > now()
  27.  
  28. window w as (PARTITION BY brand_name, cat_no ORDER BY normalize_package(package_value))
  29. ),
  30.  
  31. tmp2 as (
  32. SELECT
  33. *,
  34. max(成本异常) OVER(PARTITION BY 品牌, 货号) 产品成本异常
  35. FROM tmp
  36. )
  37.  
  38. SELECT
  39. *
  40. FROM tmp2
  41.  
  42. where 产品成本异常 = 1
  43.  
  44. ORDER BY 品牌, 货号, mg;
  45.  
  46. -- 价格异常
  47. with tmp as (
  48. select
  49. id,
  50. brand_name 品牌,
  51. cat_no 货号,
  52. package_value 规格,
  53. normalize_package(package_value) mg,
  54. round("cost",2) 成本,
  55. price 市场价,
  56. price_expiry_date 价格有效期,
  57. lag(normalize_package(package_value), 1) OVER(w) 上一个mg,
  58. lag(price, 1) OVER(w) 上一个价格,
  59. lead(normalize_package(package_value), 1) OVER(w) 下一个mg,
  60. lead(price, 1) OVER(w) 下一个价格,
  61. case when lag(price, 1) OVER(w) > price then 1
  62. else 0 end
  63. 市场价异常
  64.  
  65. FROM t_product_package pkg
  66. where pkg.brand_name ilike 'cato'
  67. and price is not null
  68. and is_delete = 1
  69. and price_expiry_date > now()
  70.  
  71. window w as (PARTITION BY brand_name, cat_no ORDER BY normalize_package(package_value))
  72. ),
  73.  
  74. tmp2 as (
  75. SELECT
  76. *,
  77. max(市场价异常) OVER(PARTITION BY 品牌, 货号) 产品价格异常
  78. FROM tmp
  79. )
  80.  
  81. SELECT
  82. *
  83. FROM tmp2
  84.  
  85. where 产品价格异常 = 1
  86.  
  87. ORDER BY 品牌, 货号, mg;
  88.  
  89.  
  90. -- 货期异常
  91. with tmp as (
  92. select
  93. id,
  94. brand_name 品牌,
  95. cat_no 货号,
  96. package_value 规格,
  97. normalize_package(package_value) mg,
  98. round("cost",2) 成本,
  99. price 市场价,
  100. delivery 货期,
  101. substring(normalize_delivery(delivery) from '(\d+)天') "货期(天)",
  102. price_expiry_date 价格有效期,
  103. lag(normalize_package(package_value), 1) OVER(w) 上一个mg,
  104. lag(substring(normalize_delivery(delivery) from '(\d+)天'), 1) OVER(w) 上一个货期,
  105. lead(normalize_package(package_value), 1) OVER(w) 下一个mg,
  106. lead(substring(normalize_delivery(delivery) from '(\d+)天'), 1) OVER(w) 下一个货期,
  107. case when lag(substring(normalize_delivery(delivery) from '(\d+)天'), 1) OVER(w) > substring(normalize_delivery(delivery) from '(\d+)天') then 1
  108. else 0 end
  109. 货期异常
  110.  
  111. FROM t_product_package pkg
  112.  
  113. where pkg.brand_name ilike 'cato'
  114. and (cost is not null
  115. or price is not null)
  116. and is_delete = 1
  117. AND price_expiry_date > now()
  118.  
  119. window w as (PARTITION BY brand_name, cat_no ORDER BY normalize_package(package_value))
  120. ),
  121.  
  122. tmp2 as (
  123. SELECT
  124. *,
  125. max(货期异常) OVER(PARTITION BY 品牌, 货号) 产品货期异常
  126. FROM tmp
  127. )
  128.  
  129. SELECT
  130. *
  131. FROM tmp2
  132.  
  133. where 产品货期异常 = 1
  134.  
  135. ORDER BY 品牌, 货号, mg
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement