鉴于此结果集,我如何基于唯一的{{},仅提取2
列中数字大于1
的那些条目,而没有提取mdline
的条目1}}列?
SQL小提琴在这里:http://sqlfiddle.com/#!18/8c17e/2
查询:
mmatter
结果:
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 |
+---------+---------------+--------+-------------------------+
请注意,该结果集中的行的+-----+---------------+---+-------------------------+
| 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 |
+-----+---------------+---+-------------------------+
值不小于2。
我尝试过此操作,但可能由于嵌套太多查询而导致语法错误?
mdline
答案 0 :(得分: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
答案 1 :(得分:0)
我将使用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
);