@cptedward_kenway
Как жить, если Константин Цветков не ответил?

Как правильно составить запрос?

spoiler
634ec8a1bc744984594589.png
задача
spoiler
4. Assuming that the current date in the system is today
b) find the differences in the prices between zone 1 and 2 for all items on the current date. See below result set example:

К этой задаче есть мое решение
spoiler
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

;

я бы хотел избавить от строк, дата которых меньше последней записи. На примере item 050234062, дата последней записи в зоне 1 21.12.18, а в зоне 2 30.12.18, но эти строки не единственные и идут не первыми (видимо с order by нужно подумать)
  • Вопрос задан
  • 59 просмотров
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы