with zone_1 as
(select item
, zone
, action_date
, selling_retail as sr_1
, max(action_date) over(partition by item, zone) as max_date
, ZONE_FUTURE_RETAIL_ID
from rpm_zone_future_retail
where zone < 2
group by item
, zone
, action_date
, selling_retail
, ZONE_FUTURE_RETAIL_ID
order by item
, zone)
,
zone_2 as
(select item
, zone
, action_date
, selling_retail as sr_2
, max(action_date) over(partition by item, zone) as max_date
, ZONE_FUTURE_RETAIL_ID
from rpm_zone_future_retail
where zone < 3 and zone > 1
group by item
, zone, action_date
, selling_retail
, ZONE_FUTURE_RETAIL_ID
order by item
, zone)
select zone_1.item
, sr_1 as price_zone_1
--, max(zone_1.action_date)
--, zone_1.zone as zZ1
--, zone_2.item
, sr_2 as price_zone_2
--, max(zone_2.action_date)
--, zone_2.zone as zZ2
, abs(sr_1 - sr_2) as difference
from zone_1, zone_2, rpm_zone_future_retail rzfr
where zone_1.item = zone_2.item and zone_1.action_date
group by zone_1.item
, sr_1
, zone_1.action_date
, zone_1.zone
, zone_2.item
, sr_2
, zone_2.action_date
, zone_2.zone
order by zone_1.item, zone_1.action_date, zone_2.action_date DESC
;