Advertisement
Guest User

Untitled

a guest
Feb 17th, 2020
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.24 KB | None | 0 0
  1. select allotment.product_id,allotment.allotment_period,balance_period, coalesce(allotment.reassort,0)+coalesce(allotment.initial_stock,0)-coalesce(quantity.quantity,0) as balance ,allotment.reassort, allotment.initial_stock,quantity.quantity,quantity.consumption_date
  2. from allotment
  3. left join lateral (
  4. select
  5. (product_unavailability.unavailability_period)::daterange as balance_period
  6. from
  7. product_unavailability
  8. where allotment.product_id = product_unavailability.product_id
  9. and allotment_period::daterange && product_unavailability.unavailability_period=true
  10. ) as unavailability_period on true
  11. left join lateral (
  12. select
  13. SUM(coalesce(quantity,0)) as quantity,consumption_date
  14. from
  15. product_allotment_usage
  16. where allotment.product_id = product_allotment_usage.product_id
  17. and allotment_period::daterange @> product_allotment_usage.consumption_date
  18. group by consumption_date,product_id,quantity) as quantity on true
  19. inner join (
  20. select
  21. "product_id",
  22. "price_period",
  23. json_agg(sub_periods) filter (
  24. where price is not null)as price_info
  25. from
  26. (
  27. select
  28. "product_price_period"."id",
  29. "product_price_period"."price_period",
  30. "product_price_period"."product_id",
  31. json_build_object( 'sub_period', product_price_sub_period.price_sub_period) as sub_periods,
  32. (
  33. select
  34. case
  35. when product_price_period.price_mode_id = 1 then (
  36. select
  37. price
  38. from
  39. price_per_occupation
  40. where
  41. product_price_period_id = product_price_period.id
  42. and price notnull
  43. limit 1)
  44. when product_price_period.price_mode_id = 2 then (
  45. select
  46. fit_price
  47. from
  48. fixed_price
  49. where
  50. product_price_period_id = product_price_period.id
  51. and fit_price notnull
  52. limit 1)
  53. when product_price_period.price_mode_id = 3 then (
  54. select
  55. fit_price
  56. from
  57. price_per_pax
  58. where
  59. product_price_period_id = product_price_period.id
  60. and fit_price notnull
  61. limit 1)
  62. when product_price_period.price_mode_id = 4 then (
  63. select
  64. fit_price
  65. from
  66. price_per_age
  67. where
  68. product_price_period_id = product_price_period.id
  69. and fit_price notnull
  70. limit 1)
  71. end) as price
  72. from
  73. "product_price_period"
  74. inner join "product_price_sub_period" on
  75. "product_price_sub_period"."product_price_period_id" = "product_price_period"."id"
  76. group by
  77. "product_price_period"."id",
  78. product_price_period.price_period,
  79. "product_price_sub_period"."price_sub_period") as "price_info"
  80. group by
  81. "product_id",price_info.price_period) as "product_price_info" on
  82. "product_price_info"."product_id" = allotment.product_id
  83. and
  84. product_price_info.price_period @> allotment_period = true
  85. where allotment.product_id = 24155
  86. --where allotment.product_id=21430
  87. and allotment.allotment_period && $1 =true
  88.  
  89. and coalesce(allotment.reassort,0)+coalesce(allotment.initial_stock,0)-coalesce(quantity.quantity,0) <=0
  90. and price_info notnull
  91. group by allotment.product_id,allotment.reassort, allotment.allotment_period,allotment.initial_stock,quantity.quantity,quantity.consumption_date,balance_period
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement