仅当找到多次时才选择 sql
我有一个包含用户 ID 和功能的表。我只想选择那些选择了 1 个以上功能的用户。查看表格
declare @tbl table (userid int, feature nvarchar(10))
insert into @tbl(userid, feature)
select 1, 'abc'
insert into @tbl(userid, feature)
select 1, 'xyz'
insert into @tbl(userid, feature)
select 2, 'abc'
insert into @tbl(userid, feature)
select 3, 'abc'
insert into @tbl(userid, feature)
select 3, 'xyz'
insert into @tbl(userid, feature)
select 3, 'pqr'
我的查询应该只返回计数 = 2(用户 ID 1 和 3,因为它们都具有多个关联的功能)。换句话说,我想要具有与其关联的多个功能的用户 ID 的计数。
I have a table with userid and feature. I want to select only those users with more than 1 feature selected. check out the table
declare @tbl table (userid int, feature nvarchar(10))
insert into @tbl(userid, feature)
select 1, 'abc'
insert into @tbl(userid, feature)
select 1, 'xyz'
insert into @tbl(userid, feature)
select 2, 'abc'
insert into @tbl(userid, feature)
select 3, 'abc'
insert into @tbl(userid, feature)
select 3, 'xyz'
insert into @tbl(userid, feature)
select 3, 'pqr'
My query should only return the count = 2 (userids 1 and 3 as they both have multiple features associated). In other words I want the count of userids with multiple features associated with them.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以尝试在
HAVING
中使用带有条件聚合函数的JOIN
You can try to use
JOIN
with condition aggregate function inHAVING
这是一个简单的聚合:用户和计数组组。
That's a simple aggregation: group by user and count.