Advertisement
AntonHuretskyi

Untitled

Nov 20th, 2022
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. insert into cdm.dm_settlement_report (restaurant_id, restaurant_name, settlement_date, orders_count, orders_total_sum, orders_bonus_payment_sum, orders_bonus_granted_sum, order_processing_fee, restaurant_reward_sum)
  2. select
  3.      r.restaurant_id
  4.     ,r.restaurant_name
  5.     ,t."date"
  6.     ,fps.count
  7.     ,fps.total_sum
  8.     ,fps.bonus_payment
  9.     ,fps.bonus_grant
  10.     ,fps.total_sum*0.25 as order_processing_fee
  11.     ,fps.total_sum - (fps.total_sum*0.25) - bonus_payment as restaurant_reward_sum
  12. from dds.fct_product_sales fps
  13. left join dds.dm_orders o on fps.order_id = o.id
  14. left join dds.dm_restaurants r on o.restaurant_id = r.id
  15. left join dds.dm_timestamps t on o.timestamp_id = t.id
  16. where o.order_status = 'CLOSED'
  17. ON CONFLICT (restaurant_id, settlement_date) DO UPDATE
  18. SET --restaurant_name = EXCLUDED.restaurant_name,
  19.     orders_count = EXCLUDED.orders_count,
  20.     orders_total_sum = EXCLUDED.orders_total_sum,
  21.     orders_bonus_payment_sum = EXCLUDED.orders_bonus_payment_sum,
  22.     orders_bonus_granted_sum = EXCLUDED.orders_bonus_granted_sum;
  23.     --order_processing_fee = EXCLUDED.order_processing_fee,
  24.     --restaurant_reward_sum = EXCLUDED.restaurant_reward_sum;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement