четверг, 2 апреля 2015 г.

SQL WITH expression в мощных RDBM

Задачка 14 из http://www.sql-ex.ru/learn_exercises.php
Задание: 14 (Serge I: 2012-04-20)
Найти производителей, которые выпускают более одной модели, при этом все выпускаемые производителем модели являются продуктами одного типа.
Вывести: maker, type


with PrdCountByType as (select 
  maker, type, count(model) as prd_count from Product 
group by 
  maker, type 
),
MakerWithCounts as (select maker, count(type) as c_type, sum(prd_count) as prd_count from PrdCountByType
group by 
  maker
having 
  count(type) < 2 and sum(prd_count) > 1)
Select distinct maker, type from Product where maker in (select maker from MakerWithCounts)


Комментариев нет:

Отправить комментарий