You are on page 1of 1

CREATE TEMP TABLE WA AS (

SELECT
WAREHOUSE_ID,
MAX(physical_country) as COUNTRY_CODE
FROM monster.WAREHOUSES
WHERE region_id=2
GROUP BY 1);

create temp table orders as


(
SELECT
merchant_customer_id,
mrr.amzn_fulfillment_center_id,
case
when mr.address_country_code in ('UK','GB') then 'UK'
else 'EU_Other'
End as to_country,
SUM(MRSI.shipped_quantity) as shipped_units
FROM FBA_RELO_REMOVAL_DDL.MR_FC_SHIPMENTS MRS
JOIN FBA_RELO_REMOVAL_DDL.MR_FC_SHIPMENT_ITEMS MRSI ON MRSI.MR_FC_SHIPMENT_ID =
MRS.MR_FC_SHIPMENT_ID
join FBA_RELO_REMOVAL_DDL.MR_FC_REQUESTS MRR ON
MRS.mr_fc_request_id=MRR.mr_fc_request_id and mrr.region_id=2
JOIN FBA_RELO_REMOVAL_DDL.MR_ORDERS MR ON MRR.mr_order_id=MR.mr_order_id and
mr.region_id=2

WHERE MRS.REGION_ID=2
and MRSI.REGION_ID=2
AND trunc(shipment_date) BETWEEN TO_DATE('2020-01-01', 'YYYY-MM-DD') AND
TO_DATE('2020-12-31', 'YYYY-MM-DD')
and mr.address_country_code is not null
and mr_fulfillment_channel in ('RETURN')
group by 1,2,3
);

SELECT
merchant_customer_id,
case when WAREHOUSE_ID in ('UK','GB') then 'UK'
else 'EU_Other'
end as from_country,
to_country,
sum(shipped_units) as shipped_units
from orders o left join WA on o.amzn_fulfillment_center_id = WA.WAREHOUSE_ID
where from_country <> to_country
group by 1,2,3
order by shipped_units desc

You might also like