Здравствуйте, gmane.comp.db.firebird.Russian!

Eсть приблизительно такая схемка - производители, товары, продавцы, 
прайс-листы (несколько колонок цен). Есть три вида скидок (1) продавца 
на конкретную единицу товара, (2) скидки продавца на товары 
определенного производителя и просто фиксированные скидки поставщика.
producer(id, name)
product(id, producer_id, name)
seller (id, name, price_category, discount_1, discount_2);
x_discounts (seller_id, product_id, discount_1, discount_2);
y_discounts (seller_id, producer_id, discount_1, discount_2);
proposals (id, product_id, seller_id, price_1, price_2)

для получения прайс-листа с учетом скидок использую примерно такой 
страшный сиквел
select
   p1.name,
   coalesce(
    price_1 * (select d1.discount_1 from x_discounts d1 where 
d1.seller_id = p1.seller_id and d1.product_id = p1.id),
    price_1 * (select d2.discount_1 from y_discounts d2 where 
d2.seller_id = p1.seller_id and d2.producer_id = p1.producer_id),
    price_1 * s1.discount_1
    price_1) price_disc_1,
   coalesce(
    price_2 * (select d1.discount_2 from x_discounts d1 where 
d1.seller_id = p1.seller_id and d1.product_id = p1.id),
    price_2 * (select d2.discount_2 from y_discounts d2 where 
d2.seller_id = p1.seller_id and d2.producer_id = p1.producer_id),
    price_1) price_disc_2,
from
   product p1
   join proposals p2 on p1.id = p2.product_id
   join sellers s1 on p2.seller_id = s1.id

Можно ли такое сделать как-нибудь такую выборку пооптимальнее?

Еще аналогичным образом ищется предложение с минимальной ценой на 
определенный товар с учетом того, что с каждым из поставщиков мы можем 
работать по разной категории цен. Может тут тоже можно что-то придумать?

select
   first 1 p2.id,
   p2.seller_id,
   p2.price_category,
   case p2.price_category
     when 1 then coalsce(
    price_1 * (select d1.discount_1 from x_discounts d1 where 
d1.seller_id = p1.seller_id and d1.product_id = p1.id),
    price_1 * (select d2.discount_1 from y_discounts d2 where 
d2.seller_id = p1.seller_id and d2.producer_id = p1.producer_id),
    price_1 * s1.discount_1
    price_1)
     when 2 then coalsce(
    price_2 * (select d1.discount_2 from x_discounts d1 where 
d1.seller_id = p1.seller_id and d1.product_id = p1.id),
    price_2 * (select d2.discount_2 from y_discounts d2 where 
d2.seller_id = p1.seller_id and d2.producer_id = p1.producer_id),
    price_1)
   end price,

   case p2.price_category
   end discount
form
   product p1
   join proposals p2 on p1.id = p2.product_id
   join sellers s1 on p2.seller_id = s1.id
where
   p1.id = :product_id
order by
   4

-- 
С наилучшими пожеланиями,
Николай Войнов.


--~--~---------~--~----~------------~-------~--~----~
-~----------~----~----~----~------~----~------~--~---

Ответить