0

我试图在类型不是上诉人 Rep 1 的表格中查找所有内容。错误来自以下事实: ayear和表格中可以有很多行。这是我尝试过的:caseseqnumbersyearcaseseqnumber

Select caseseqnumber, year
from caseparticipants
where not exists (Select *
                  from caseparticipants
                  where participanttype = 'Appellant Rep 1')

有什么帮助吗?!

4

5 回答 5

2

为什么需要在那里进行嵌套搜索。只有在检查多个数据库表中的内容时才需要嵌套搜索。

依照

select caseqnumber,year from caseparticipants where paticipanttype <> 'Appellant Rep 1'

(<> 是 NOT EQUAL TO 的 sql 子句)

于 2013-05-02T12:16:31.793 回答
1
Select 
    caseseqnumber, 
    year 
from 
    caseparticipants 
where 
    participanttype != 'Appellant Rep 1'
于 2013-05-02T12:16:23.983 回答
0

如果您想要所有有上诉人但没有代表的案件:

Select caseseqnumber, year
from caseparticipants cp1
where not exists (Select null
                  from caseparticipants cp2
                  where cp2.participanttype = 'Appellant Rep 1'
                  and cp1.caseseqnumber = cp2.caseseqnumber
                  and cp1.year = cp2.year
)
于 2013-05-02T12:30:11.913 回答
0

您可以使用 LEFT JOIN 将 caseparticipants 与自身连接起来。如果连接不成功,则表示 caseeqnumber 和 year 没有一行participanttype = 'Appellant Rep 1'

SELECT
  c1.caseseqnumber, 
  c1.year 
FROM 
  caseparticipants c1 LEFT JOIN caseparticipants c2
  ON c1.year=c2.year AND c1.caseseqnumber=c2.caseseqnumber
     AND c2.participanttype = 'Appellant Rep 1'
WHERE
  c2.year IS NULL

编辑

要比较 caseeqnumber、year 的不同组合的数量以及具有“Appellant Rep 1”类型的组合的数量,您可以使用以下 SQL Server 查询:

SELECT
  COUNT(DISTINCT
    CAST(c1.caseseqnumber AS VARCHAR) + '-' + CAST(c1.year AS VARCHAR)),
  COUNT(DISTINCT
    CAST(c2.caseseqnumber AS VARCHAR) + '-' + CAST(c2.year AS VARCHAR))
FROM 
  caseparticipants c1 LEFT JOIN caseparticipants c2
  ON c1.year=c2.year AND c1.caseseqnumber=c2.caseseqnumber
     AND c2.participanttype = 'Appellant Rep 1'
于 2013-05-02T12:26:42.890 回答
0

为什么不尝试WHERE直接在主查询中使用条件而不是使用子查询?

SELECT caseseqnumber, year 
  FROM caseparticipants 
 WHERE participanttype != 'Appellant Rep 1'
于 2013-05-02T12:16:30.253 回答