根据数字过滤结果集

Filtering result set based on a number

给定此结果集,我如何根据唯一性仅提取那些编号大于 2 但在 mdline 列中没有 1 条目的条目mmatter 列?

SQL Fiddle 这里:http://sqlfiddle.com/#!18/8c17e/2

查询:

SELECT top 1000
       [mdindex]
      ,[mmatter]
      ,[mdline]
      ,[mddesc]
FROM [desc]
WHERE [mmatter] IN (
    SELECT [mmatter]
    FROM [desc]
    GROUP BY [mmatter]
     HAVING COUNT(distinct [mdline]) > 1
)
order by mmatter

结果:

+---------+---------------+--------+-------------------------+
| mdindex | mmatter       | mdline | mddesc                  |
+---------+---------------+--------+-------------------------+
| 142     | X30539.000021 | 1      | Avocet, pied            |
+---------+---------------+--------+-------------------------+
| 143     | X30539.000021 | 2      | Margay                  |
+---------+---------------+--------+-------------------------+
| 111     | X30820.004199 | 1      | African buffalo         |
+---------+---------------+--------+-------------------------+
| 112     | X30820.004199 | 2      | Siskin, pine            |
+---------+---------------+--------+-------------------------+
| 113     | X30820.004199 | 3      | African jacana          |
+---------+---------------+--------+-------------------------+
| 114     | X30820.0042   | 2      | Caracara, yellow-headed |
+---------+---------------+--------+-------------------------+
| 115     | X30820.0042   | 3      | Whip-tailed wallaby     |
+---------+---------------+--------+-------------------------+
| 116     | X30820.0042   | 4      | Greater rhea            |
+---------+---------------+--------+-------------------------+
| 120     | X30820.004202 | 1      | Nuthatch, red-breasted  |
+---------+---------------+--------+-------------------------+
| 121     | X30820.004202 | 2      | Arctic tern             |
+---------+---------------+--------+-------------------------+
| 122     | X30820.004202 | 3      | Tyrant flycatcher       |
+---------+---------------+--------+-------------------------+
| 123     | X30820.004203 | 1      | Plover, three-banded    |
+---------+---------------+--------+-------------------------+
| 124     | X30820.004203 | 2      | Tortoise, radiated      |
+---------+---------------+--------+-------------------------+
| 129     | X30820.004204 | 2      | Laughing dove           |
+---------+---------------+--------+-------------------------+
| 130     | X30820.004204 | 3      | Iguana, marine          |
+---------+---------------+--------+-------------------------+

这是我要return的数据,来自上面的结果集:

+-----+---------------+---+-------------------------+
| 114 | X30820.0042   | 2 | Caracara, yellow-headed |
+-----+---------------+---+-------------------------+
| 115 | X30820.0042   | 3 | Whip-tailed wallaby     |
+-----+---------------+---+-------------------------+
| 116 | X30820.0042   | 4 | Greater rhea            |
+-----+---------------+---+-------------------------+
| 129 | X30820.004204 | 2 | Laughing dove           |
+-----+---------------+---+-------------------------+
| 130 | X30820.004204 | 3 | Iguana, marine          |
+-----+---------------+---+-------------------------+

请注意,此结果集中的行没有 mdline 小于 2 的值。

我试过了,但出现语法错误,可能是因为嵌套了太多查询?

SELECT * FROM  
    ( SELECT top 1000
      [mdindex]
          ,[mmatter]
          ,[mdline]
          ,[mddesc]
    FROM [desc]
    WHERE [mmatter] IN (
        SELECT [mmatter]
        FROM [desc]
        GROUP BY [mmatter]
         HAVING COUNT(distinct [mdline]) > 1
        )
        order by mmatter
    )
WHERE mdline not like '1'

您可以通过在 where 子句中添加 not exists 来获得所需的结果:

SELECT [mdindex]
    ,[mmatter]
    ,[mdline]
    ,[mddesc]
FROM [table1] t0
WHERE [mmatter] IN (
    SELECT [mmatter]
    FROM [table1]
    GROUP BY [mmatter]
    HAVING COUNT(distinct [mdline]) > 1
)
AND NOT EXISTS
(
    SELECT 1
    FROM [table1] t1
    WHERE t0.mmatter = t1.mmatter
    AND t0.mdline = 1
)
ORDER BY mmatter

我会使用 exists & not exists :

select t1.*
from table1 t1
where not exists (select 1 
                  from table1 t2
                  where t2.mmatter = t1.mmatter and t2.mdline = 1 
                  ) and
       exists (select 1 
               from table1 t2
               where t2.mmatter = t1.mmatter and t2.mdline <> t1.mdline
               );