DmitryAnt

Рейтинг
8
Регистрация
19.05.2011

Данный запрос выводит группы с минимальной ценой, не равной 0, но при этом исключает группы, в которых все товары с нулевой ценой:


select t.* from
(
select g.id,g.title,min(c.price)
from groups g join catalog c on g.title=c.type
where price>0 group by g.id,g.title
) t

Запрос выводит группы, в которых все цены =0 (проверка осуществляется функцией среднего значения)


select t.*
from
(
select g.id,g.title,avg(c.price) a
from groups g join catalog c on g.title=c.type
group by g.id,g.title
) t
where t.a=0

Теперь два запроса объединяем


select t.* from (select g.id,g.title,min(c.price) from groups g join catalog c on g.title=c.type where price>0 group by g.id,g.title) t
union
select t.* from (select g.id,g.title,avg(c.price) a from groups g join catalog c on g.title=c.type group by g.id,g.title) t where t.a=0
Всего: 51