我是 Oracle 的新手。我有一个表,其中有多个分配给用户的限制组。每组用户都属于一个 userregionID。 我必须显示一个 userregionID 列表,其中为用户分配了 1 个以上的限制组。
我的桌子
user - Id, userregionid
userRestriction - userId, restrictionGroup
例如
用户表
EID-999 | 12345
EID- 888 | 12345
D-900 | 2322
F-943 | 6767
用户限制表
UserId | RestrictionGroup
EID-999| A1
EID-888 | B1
EID-999 | C1
F-943 | Z1
F-943 | X1
所以,我的输出应该是这样的
UserRegionId | Count of Users having restriction Group >1
12345 | 1
6767 | 1
因为用户 EID-999
和 F-943
分别属于 userregionId 12345
和 6767
,并且他们被分配了 1 个以上的限制组。
我的努力
我编写了一个查询,用于显示在同一 userregionID 内具有 > 1 个限制组的用户列表 但我对如何进一步处理并将此查询转换为只能从整个数据库中获取计数和 userregionID 的嵌套查询一无所知。
我的查询
select distinct ec.userId, e.userregionid,
count(distinct ec.restrictionGroup) over (partition by ec.userId)
from user e, userRestriction ec
where e.userregionid = '12345' and e.Id= ec.userId
答案 0 :(得分:0)
此处您可能不需要嵌套查询,下面的 INNER JOIN
可以帮助您。
select u.userregionid, count(ur.userId)
from userRestriction ur, USR u
where ur.userId=u.id
group by ur.userId , u.userregionid
having count(ur.userId) >1;
PS:此处的 DB-Fiddle 可以帮助您进行可视化。