SQL - 过滤结果以匹配两个不同行中的两个列

时间:2016-01-28 01:55:31

标签: sql select

我正在进行SQL测验,如下所示: 编写一个SQL语句来检索所有在相同项目上工作的人员,每个项目都需要相同的小时数。使用抽样数据,只能检索史密斯和布朗。奥克斯被取消资格,因为奥克沙斯只为项目Y工作了10个小时(而不是史密斯的20个)

表格:

|  name | project | hours |
|-------|---------|-------|
| Smith |       X |    10 |
| Smith |       Y |    20 |
|   Doe |       Y |    20 |
| Brown |       X |    10 |
|   Doe |       Z |    30 |
| Chang |       X |    10 |
| Brown |       Y |    20 |
| Brown |       A |    10 |
| Woody |       X |    10 |
| Woody |       Y |    10 |

我想出了这个:

SELECT * INTO #temp 
FROM workson 
WHERE name='smith'
SELECT * from workson as w 
WHERE project IN 
  (SELECT project FROM #temp
  WHERE project=w.project AND hours=w.hours )
DROP TABLE #temp

结果:

name    project hours
Smith   X       10
Smith   Y       20
Doe     Y       20
Brown   X       10
Chang   X       10
Brown   Y       20
Woody   X       10

但问题是只有史密斯和布朗才会被退回。我无法弄清楚如何以任何优雅的方式过滤掉其他人。

感谢。

3 个答案:

答案 0 :(得分:2)

select t1.*
from workson t1
inner join workson t2 on t2.name = 'Smith' and t2.project = t1.project and t2.hours = t1.hours
where t1.name in
(
  select i1.name
  from workson i1
  inner join workson i2 on i2.name = 'Smith' and i2.project = i1.project and i2.hours = i1.hours
  group by i1.name
  having count(*) = (select count(*) from workson where name = 'Smith')
)

http://sqlfiddle.com/#!3/74566/2/0

答案 1 :(得分:1)

我在上面的答案中遇到了一些问题,但是它给了我一个非常好的框架,所以我不能真正赞同这个答案:

SELECT name, project, hours FROM workson w2
WHERE name IN 
(SELECT name FROM workson w
INNER JOIN 
(SELECT project, hours FROM workson
WHERE name = 'Smith') q1
ON q1.project = w.project AND q1.hours = w.hours
GROUP BY w.name
HAVING COUNT (*) = (SELECT COUNT(*) FROM workson WHERE name = 'Smith'))
AND project IN (SELECT project FROM workson WHERE name = 'Smith')

答案 2 :(得分:0)

此解决方案是已接受答案的替代方案。它使用两个INNER JOIN操作来实现最终结果。 SELECT子查询标识与“Smith”具有相同项目/小时集的名称。

SELECT t2.name, t1.project, t1.hours
FROM workson t1
INNER JOIN workson t2
     ON t1.project = t2.project AND t1.hours = t2.hours
INNER JOIN
(
    SELECT w2.name
    FROM workson w1
    INNER JOIN workson w2
        ON w1.project = w2.project AND w1.hours = w2.hours
    WHERE w1.name = 'Smith'
    GROUP BY w2.name
    HAVING COUNT(*) = (SELECT COUNT(*) FROM workson WHERE name = 'Smith')
) t3
    ON t2.name = t3.name
WHERE t1.name = 'Smith'

按照以下链接进行正在运行的演示:

SQLFiddle