最小重合次数大于

时间:2016-01-22 09:30:57

标签: sql count distinct having

我不知道如何解决这个问题:有3个表(项目,工程师和客户)。我想知道已经雇用2个或更多不同工程师的客户的名字,他们的收入超过X.

表“project”包含客户端和工程师的id以及其他参数。有关客户名称和工程师工资的信息分别包含在“客户”和“工程师”中。

select name 
from client, project
where project.client_number IN
    (
    select p.client_number
    from project p inner join engineer e on (p.eng_number=e.eng_number)
    where e.salary>50000
    group by p.client_number
    having count (distinct p.eng_number)>2
    )

group by client.name
;

2 个答案:

答案 0 :(得分:1)

你很亲密:

select name 
from client
where client_number IN
  (
    select p.client_number
    from project p inner join engineer e on (p.eng_number=e.eng_number)
    where e.salary>50000
    group by p.client_number
    having count (distinct p.eng_number)>2
  )

答案 1 :(得分:0)

我不确定我是否理解正确但这种方法应该稍作修改: 从工程师收入超过50000开始:

(select * from engineer where salary>50000 ) e

加入项目表:

inner join 
(select * from projects ) p -- this is overkill - just to give an option to filter on this level can be replaced by projects p
on (p.eng_number=e.eng_number)

现在加入客户表

inner join 
(select * from clients ) c  -- this is overkill - just to give an option to filter on this level. can be replaced by clients c
on (p.client_number=e.client_number) 

现在你可以用一个带有任何过滤器的外部选择和

分组来包装它
select name from (
(select * from engineer where salary>50000 ) e
inner join 
(select * from projects ) p 
on (p.eng_number=e.eng_number)
inner join 
(select * from clients ) c  
on (p.client_number=e.client_number)
) epc
group by c.client_number
having count (distinct p.eng_number)>2