Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/76.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,我有一个表,其中有多个重复条目的行,例如 |[com_enq_id]| [comtype_type] | | 1 | blah | | 1 | Found DP Quotes | | 1 | Found Quotes | | 2 | Found DP Quotes | | 2 | blah | | 2 | Found DP Q

我有一个表,其中有多个重复条目的行,例如

|[com_enq_id]|  [comtype_type]  |
|  1         | blah             |
|  1         | Found DP Quotes  |
|  1         | Found Quotes     |
|  2         | Found DP Quotes  |
|  2         | blah             |
|  2         | Found DP Quotes  |
|  3         | Found DP Quotes  |
|  3         | Found DP Quotes  |
|  3         | Found DP Quotes  |
我试图仅从表中选择
[com\u enq\u id]
没有
找到的DP引号
找到的引号
的行。应选择具有其中一个或无一个的ID,但不应选择同时具有这两个ID的ID。(例如,在这种情况下,仅应选择
2
3

我尝试了以下我起初认为有效的方法,但如果该ID有其他
[comtype\u type]
(例如
blah
)的任何其他行,它也会返回带有这两种方法的任何行:

选择
t1.[com_enq_id]
,t1.[comtype\u-type],t2.[comtype\u-type]
从[comments_view]t1
加入[comments_view]t2
在t1上。[com_enq_id]=t2。[com_enq_id]
哪里
(t1.[comtype\u type]=“找到的引号”和t2.[comtype\u type]“找到的DP引号”)
或
(t2.[comtype\u type]=“找到的引号”和t1.[comtype\u type]“找到的DP引号”)

有谁能指出我需要如何修改它以排除那些行/只选择没有这两个行的
[com\u enq\u id]
吗?

我认为处理这类问题最简单、最灵活的方法是使用
分组方式和
拥有
。要获取
com\u enq\u id的列表

select cv.com_enq_id
from comments_view cv
where comtype_type in ('Found DP Quotes', 'Found Quotes')
group by cv.com_enq_id
having count(distinct comtype_type) <> 2;

我认为处理这类问题最简单、最灵活的方法是使用
分组方式和
拥有方式。要获取
com\u enq\u id的列表

select cv.com_enq_id
from comments_view cv
where comtype_type in ('Found DP Quotes', 'Found Quotes')
group by cv.com_enq_id
having count(distinct comtype_type) <> 2;

非常感谢,先生。非常感谢,先生。
select cv.*
from comments_view cv
where cv.com_enq_id in (select cv.com_enq_id
                        from comments_view cv
                        where comtype_type in ('Found DP Quotes', 'Found Quotes')
                        group by cv.com_enq_id
                        having count(distinct comtype_type) <> 2
                       );