create table Employees (id number unique, name text);
create table Services (Sid number unique, Sname text);
create table EmployeesServices (EEid number, ESid number);
insert into Employees values (1, 'bob');
insert into Employees values (2, 'joan');
insert into Employees values (3, 'philippe');
insert into Services values (100, 'fell tree');
insert into Services values (101, 'row boat');
insert into Services values (102, 'read book');
insert into Services values (103, 'sell book');
insert into Services values (104, 'sell ticket');
insert into EmployeesServices values (1, 100);
insert into EmployeesServices values (2, 101);
insert into EmployeesServices values (2, 101);
insert into EmployeesServices values (3, 101);
insert into EmployeesServices values (3, 103);
insert into EmployeesServices values (3, 103);
insert into EmployeesServices values (3, 104);
insert into EmployeesServices values (1, 100);
insert into EmployeesServices values (1, 101);
insert into EmployeesServices values (1, 102);
insert into EmployeesServices values (1, 102);
insert into EmployeesServices values (1, 102);
insert into EmployeesServices values (1, 102);
select count(*) as Number_of_Unique_Services_Available from Services;
select id, name, count(distinct Sid) as Unique_Services_Performed
from Employees, Services, EmployeesServices
where id = EEid and Sid = ESid
group by id
order by Unique_Services_Performed desc, name;
テスト用にいくつかのテーブルを作成しました。従業員は同じサービスを複数回実行できると想定しています。最初の select ステートメントは、利用可能な一意のサービスの数を示します。2 つ目は、各従業員が実行した一意のサービスの数を示す出力を作成します。以下のサンプル出力は、5 つの固有のサービスを示しており、そのすべてを実行した人はいません。
Number_of_Unique_Services_Available
-----------------------------------
5
id name Unique_Services_Performed
---------- ---------- -------------------------
1 bob 3
3 philippe 3
2 joan 1