Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 如何基于纯匹配条件应用过滤器?_Sql_Sql Server - Fatal编程技术网

Sql 如何基于纯匹配条件应用过滤器?

Sql 如何基于纯匹配条件应用过滤器?,sql,sql-server,Sql,Sql Server,从下表中,我想筛选纯粹属于类别1的员工 +---------+----------+ | EMPNAME | Category | +---------+----------+ | aaa | 1 | | aaa | 2 | | aaa | 3 | | bbb | 2 | | bbb | 3 | | ccc | 1 | | ddd | 1 |

从下表中,我想筛选纯粹属于类别1的员工

+---------+----------+
| EMPNAME | Category |
+---------+----------+
| aaa     |        1 |
| aaa     |        2 |
| aaa     |        3 |
| bbb     |        2 |
| bbb     |        3 |
| ccc     |        1 |
| ddd     |        1 |
| ddd     |        2 |
| eee     |        1 |
| fff     |        3 |
| ggg     |        2 |
| hhh     |        1 |
| hhh     |        2 |
| hhh     |        3 |
| iii     |        2 |
| jjj     |        3 |
| kkk     |        1 |
+---------+----------+
已尝试where子句,但员工aaa也会出现错误的结果。

您可以使用group by和having count

select * from yourtable t1
where not exists
(
    select 1
    from yourtable t2
    where t1.EMPNAME=t2.EMPNAME
    and t2.Category<>1
)
除以下情况外使用:


这是什么?aaa 1aaa是具有类别1、2和3的员工姓名。您的预期结果是什么。实际上不需要类别=1。
select * from table1 where empname in (select empname from table1 
group by empname 
having count(category) = 1) and category = 1
SELECT EMPNAME
FROM table
WHERE Category = 1

EXCEPT

SELECT EMPNAME
FROM table
WHERE Category <> 1