Advertisement
AntonHuretskyi

Untitled

Nov 29th, 2022
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. WITH dsra AS (
  2.     SELECT
  3.         --dsra.restaurant_id,
  4.         --dsra.restaurant_name,
  5.         --dsra.orders_total_sum
  6.         *
  7.     FROM public_test.dm_settlement_report_actual dsra
  8. ),
  9. dsre AS (
  10.     SELECT
  11.         --dsre.restaurant_id,
  12.         --dsre.restaurant_name,
  13.         --dsre.orders_total_sum
  14.         *
  15.     FROM public_test.dm_settlement_report_expected dsre
  16. ),
  17. DIFFS AS (
  18.     SELECT
  19.         COALESCE(dsra.restaurant_id, dsre.restaurant_id) AS restaurant_id,
  20.         COALESCE(dsra.restaurant_name, dsre.restaurant_name) AS restaurant_name,
  21.         dsra.orders_count as orders_count_act,
  22.         dsre.orders_count as orders_count_exp,
  23.         COALESCE(dsra.orders_count, 0) - COALESCE(dsre.orders_count, 0) AS orders_count_diff,
  24.         dsra.orders_total_sum AS orders_total_sum_act,
  25.         dsre.orders_total_sum AS orders_total_sum_exp,
  26.         COALESCE(dsra.orders_total_sum, 0) - COALESCE(dsre.orders_total_sum, 0) AS orders_total_sum_diff,
  27.         dsra.orders_bonus_payment_sum AS orders_bonus_payment_sum_act,
  28.         dsre.orders_bonus_payment_sum AS orders_bonus_payment_sum_exp,
  29.         COALESCE(dsra.orders_bonus_payment_sum, 0) - COALESCE(dsre.orders_bonus_payment_sum, 0) AS orders_bonus_payment_sum_diff,
  30.         dsra.orders_bonus_granted_sum AS orders_bonus_granted_sum_act,
  31.         dsre.orders_bonus_granted_sum AS orders_bonus_granted_sum_exp,
  32.         COALESCE(dsra.orders_bonus_granted_sum, 0) - COALESCE(dsre.orders_bonus_granted_sum, 0) AS orders_bonus_granted_sum_diff,
  33.         dsra.order_processing_fee AS order_processing_fee_act,
  34.         dsre.order_processing_fee AS order_processing_fee_exp,
  35.         COALESCE(dsra.order_processing_fee, 0) - COALESCE(dsre.order_processing_fee, 0) AS order_processing_fee_diff,
  36.         dsra.restaurant_reward_sum AS restaurant_reward_sum_act,
  37.         dsre.restaurant_reward_sum AS restaurant_reward_sum_exp,
  38.         COALESCE(dsra.restaurant_reward_sum, 0) - COALESCE(dsre.restaurant_reward_sum, 0) AS restaurant_reward_sum_diff
  39.     FROM dsra
  40.     FULL JOIN dsre ON dsra.restaurant_name = dsre.restaurant_name
  41. )
  42. SELECT
  43. *
  44. FROM DIFFS
  45. where orders_count_diff <> 0 or
  46.       orders_total_sum_diff <> 0 or
  47.       orders_bonus_payment_sum_diff <> 0 or
  48.       orders_bonus_granted_sum_diff <> 0 or
  49.       order_processing_fee_diff <> 0 or
  50.       restaurant_reward_sum_diff <> 0;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement