计算查询结果
counting the result of a query
Person table
---------------------
|email (pk) | name|
---------------------
|a@hotmail.com| A |
|b@hotmail.com| B |
|c@hotmail.com| C |
Role table
---------------------------------
|Role |Power |
-------------------------------|
|Primary |20 |
|Secondary |10 |
|Supervisor |30 |
--------------------------------
Assignment table
------------------------------------------------------------------
|Team Name| Term | Role |Email |Join_date
------------------------------------------------------------------
|AA |2013_1 |Supervisor |a@hotmail.com |2013-08-05
|BB |2013_1 |Secondary |a@hotmail.com |2013-08-05
|CC |2013_1 |Supervisor |c@hotmail.com |2013-08-05
|DD |2013_1 |Secondary |a@hotmail.com |2013-08-05
|AA |2013_1 |Secondary |b@hotmail.com |2013-08-05
我的预期结果
|name | email | num_of_time_pri | num_of_time_sec | num_of_time_sup|
---------------------------------------------------------------------------------------
|A | a@hotmail.com|0 |2 | 1 |
|B | b@hotmail.com|0 |1 | 0 |
|C | c@hotmail.com|0 |0 | 1 |
给定一个术语,例如 2013_1
我必须找到所有分配给至少一个角色的人。并计算此人分配给每个角色的数量。
使用这个查询:
select
distinct p.name,
p.email
from assignment a,person p
where term ='2013_1' and
a.email = p.email;
假设它 returns 3 行,如亲眼所见 table。从那里,我想得到预期的结果 table。我如何从那里继续?
使用聚合 case
语句。并且,尽可能避免使用 implicit join
.
select
p.name,
p.email,
sum(case when role='Primary' then 1 else 0 end) as num_of_time_pri,
sum(case when role='Secondary' then 1 else 0 end) as num_of_time_sec,
sum(case when role='Supervisor' then 1 else 0 end) as num_of_time_sup
from assignment a
inner join person p on a.email = p.email
where term ='2013_1'
group by p.name,p.email;
或
select
p.name,
p.email,
count(case when role='Primary' then 1 end) as num_of_time_pri,
count(case when role='Secondary' then 1 end) as num_of_time_sec,
count(case when role='Supervisor' then 1 end) as num_of_time_sup
from assignment a
inner join person p on a.email = p.email
where term ='2013_1'
group by p.name,p.email;
Person table
---------------------
|email (pk) | name|
---------------------
|a@hotmail.com| A |
|b@hotmail.com| B |
|c@hotmail.com| C |
Role table
---------------------------------
|Role |Power |
-------------------------------|
|Primary |20 |
|Secondary |10 |
|Supervisor |30 |
--------------------------------
Assignment table
------------------------------------------------------------------
|Team Name| Term | Role |Email |Join_date
------------------------------------------------------------------
|AA |2013_1 |Supervisor |a@hotmail.com |2013-08-05
|BB |2013_1 |Secondary |a@hotmail.com |2013-08-05
|CC |2013_1 |Supervisor |c@hotmail.com |2013-08-05
|DD |2013_1 |Secondary |a@hotmail.com |2013-08-05
|AA |2013_1 |Secondary |b@hotmail.com |2013-08-05
我的预期结果
|name | email | num_of_time_pri | num_of_time_sec | num_of_time_sup|
---------------------------------------------------------------------------------------
|A | a@hotmail.com|0 |2 | 1 |
|B | b@hotmail.com|0 |1 | 0 |
|C | c@hotmail.com|0 |0 | 1 |
给定一个术语,例如 2013_1
我必须找到所有分配给至少一个角色的人。并计算此人分配给每个角色的数量。
使用这个查询:
select
distinct p.name,
p.email
from assignment a,person p
where term ='2013_1' and
a.email = p.email;
假设它 returns 3 行,如亲眼所见 table。从那里,我想得到预期的结果 table。我如何从那里继续?
使用聚合 case
语句。并且,尽可能避免使用 implicit join
.
select
p.name,
p.email,
sum(case when role='Primary' then 1 else 0 end) as num_of_time_pri,
sum(case when role='Secondary' then 1 else 0 end) as num_of_time_sec,
sum(case when role='Supervisor' then 1 else 0 end) as num_of_time_sup
from assignment a
inner join person p on a.email = p.email
where term ='2013_1'
group by p.name,p.email;
或
select
p.name,
p.email,
count(case when role='Primary' then 1 end) as num_of_time_pri,
count(case when role='Secondary' then 1 end) as num_of_time_sec,
count(case when role='Supervisor' then 1 end) as num_of_time_sup
from assignment a
inner join person p on a.email = p.email
where term ='2013_1'
group by p.name,p.email;