英文:
Return results that don't have a specific value
问题
表格信息:
有2列,personsnumber(人员编号)和their code(他们的代码)。
一个人可以有多个代码。
我的任务是找出没有9210的人。
当我写一个语句 WHERE code != 9210
时,它会返回每个人,即使那个人有9210,但我只需要那些没有9210的数字。
英文:
Table info:
There are 2 columns personsnumber and their code.
a person can have multiple codes.
I was tasked to find people that don't have the 9210
When I write a statement WHERE code != 9210
it returns every person even if that person has 9210, but I only need the numbers that don't had 9210.
答案1
得分: 1
你可以使用 not exists
:
select t.*
from table t
where not exists (select 1
from table t1
where t1.personsnumber = t.personsnumber and t1.code = 9210
);
如果只需要 personsnumber
,聚合也会有帮助:
select t.personsnumber
from table t
group by t.personsnumber
having sum(case when t.code = 9210 then 1 else 0 end) = 0;
英文:
You can use not exists
:
select t.*
from table t
where not exists (select 1
from table t1
where t1.personsnumber = t.personsnumber and t1.code = 9210
);
Aggregation would also help you if only personsnumber
is needed. :
select t.personsnumber
from table t
group by t.personsnumber
having sum(case when t.code = 9210 then 1 else 0 end) = 0;
答案2
得分: 0
你可以使用不存在 (就像Yogesh在他们的回答中所示) 或者左连接 (就像这样):
SELECT *
FROM sometable base
LEFT JOIN sometable code on base.personnumber = code.personnumber and code.code = 9210
WHERE code.personnumber is null
英文:
You can use not exists (like Yogesh shows in their answer) or a left join (like this):
SELECT *
FROM sometable base
LEFT JOIN sometable code on base.personnumber = code.personnumber and code.code = 9210
WHERE code.personnumber is null
答案3
得分: 0
以下是您要翻译的内容:
有很多种方法可以做到这一点。我将一些(不是从图像中全部输入的)数据放入了一个表变量中:
DECLARE @MyTable TABLE (PersonNumber VARCHAR(50), Code VARCHAR(50));
INSERT INTO @MyTable SELECT '4050100', '9210';
INSERT INTO @MyTable SELECT '4050100', '1020';
INSERT INTO @MyTable SELECT '2650100', '1020';
以下是获得您想要的答案的五种方法:
SELECT DISTINCT m1.PersonNumber FROM @MyTable m1 LEFT JOIN @MyTable m2 ON m2.PersonNumber = m1.PersonNumber AND m2.Code = '9210' WHERE m2.PersonNumber IS NULL;
SELECT DISTINCT PersonNumber FROM @MyTable m1 WHERE NOT EXISTS (SELECT * FROM @MyTable m2 WHERE m2.PersonNumber = m1.PersonNumber AND m2.Code = '9210');
WITH x AS (SELECT DISTINCT PersonNumber FROM @MyTable WHERE Code = '9210') SELECT DISTINCT m.PersonNumber FROM @MyTable m LEFT JOIN x ON x.PersonNumber = m.PersonNumber WHERE x.PersonNumber IS NULL;
SELECT PersonNumber FROM @MyTable GROUP BY PersonNumber HAVING MAX(CASE WHEN Code = '9210' THEN 1 END) IS NULL;
SELECT PersonNumber FROM @MyTable GROUP BY PersonNumber HAVING MAX(CASE WHEN Code = '9210' THEN 1 ELSE 0 END) = 0;
简要地说,它们的工作原理如下:
- 使用LEFT JOIN来检查是否没有相同人员编号的9210代码记录
- NOT EXISTS - 基本上是相同的逻辑,但可能更容易理解?
- 使用公共表达式首先识别9210,然后找到没有匹配项的情况
- 使用聚合
- 使用更易于理解的查询中的聚合
我进行了非常粗略的分析,发现聚合方法在性能上似乎稍微优于其他方法,其他方法性能大致相当。
英文:
There's many, many ways to do this. I put some (not typing all of that from an image) data into a table variable:
DECLARE @MyTable TABLE (PersonNumber VARCHAR(50), Code VARCHAR(50));
INSERT INTO @MyTable SELECT '4050100', '9210';
INSERT INTO @MyTable SELECT '4050100', '1020';
INSERT INTO @MyTable SELECT '2650100', '1020';
Here's five ways to get the answer you want:
SELECT DISTINCT m1.PersonNumber FROM @MyTable m1 LEFT JOIN @MyTable m2 ON m2.PersonNumber = m1.PersonNumber AND m2.Code = '9210' WHERE m2.PersonNumber IS NULL;
SELECT DISTINCT PersonNumber FROM @MyTable m1 WHERE NOT EXISTS (SELECT * FROM @MyTable m2 WHERE m2.PersonNumber = m1.PersonNumber AND m2.Code = '9210');
WITH x AS (SELECT DISTINCT PersonNumber FROM @MyTable WHERE Code = '9210') SELECT DISTINCT m.PersonNumber FROM @MyTable m LEFT JOIN x ON x.PersonNumber = m.PersonNumber WHERE x.PersonNumber IS NULL;
SELECT PersonNumber FROM @MyTable GROUP BY PersonNumber HAVING MAX(CASE WHEN Code = '9210' THEN 1 END) IS NULL;
SELECT PersonNumber FROM @MyTable GROUP BY PersonNumber HAVING MAX(CASE WHEN Code = '9210' THEN 1 ELSE 0 END) = 0;
Briefly, here's how they work:
- LEFT JOIN to check there's no record with the 9210 code for the same person number
- NOT EXISTS - basically the same logic, but might be easier to follow?
- Using a common-table expression to identify the 9210s first, then find any cases where there's no match
- Using aggregation
- Using aggregation in an easier to follow query
I did some very, very rough analysis, and the aggregation methods seem to be slightly better for performance than the other methods, which are all about equal.
答案4
得分: 0
将"Code"列的数据类型转换为nvarchar(50)。
select * from personsnumber where convert(nvarchar(50),code) != '9210';
英文:
You may convert the data type of the Code column
select * from personsnumber where convert(nvarchar(50),code) != '9210'
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论