sql - 如何在 SQL Server 中编写以下条件
问题描述
我有一张这样的桌子
create table test (custID varchar(20), currNo int)
insert into test
values ('00123', 1) ,('00123', 2), ('00123', 3),
('00124', 2), ('00124', 3),
('00125', 3),('00125', 4),
('00126', 1),('00126', 3)
我只需要选择那些具有 currNo != 1 但它可以具有 currNo > 1 的 custID
下面是我的一段代码;
select distinct custID from test
where currNo != 1 and currNo > 1
上述查询的结果:
00123
00124
00125
00126
例外结果:
00124
00125
请更正我的查询以获得所需的输出。提前致谢。
解决方案
使用简单GROUP BY
和HAVING
条件适用于currNo
SELECT custID FROM #test
GROUP BY custID
HAVING MIN(currNo)<>1
结果
custID
00124
00125
推荐阅读
- c - 错误:“asm”中的未知寄存器名称“r3”,在clobber中
- node.js - 当检测到带有 server.on 的发布请求时,Node.js 返回无法读取属性禁用未定义
- python - 如何保存从冻结推理图生成的 TensorRT 图?
- c# - WebClient.DownloadFileAsync - 我如何在下载多个文件时跟踪下载的总字节数?
- delphi - 使用 IXMLNodes 创建 XML 文档
- python - 有没有办法消除 VSCode 上的“无法导入 XXX”错误?
- html - 如何制作带有圆形边框的链接按钮
- python - 检查一个数字是否存在于python的另一个列表中
- .net - 向 Azure Batch 资源添加标签
- api - 将用户电子邮件范围添加到 Google OAuth