使用 Select 语句的嵌套查询

Nested Queries with Select Statement

我是 Oracle 的新手。我有一个 table,其中有多个分配给用户的限制组。每组用户都属于一个userregionID。 我必须显示一个 userregionID 列表,其中为用户分配了超过 1 个限制组。

我的Table

user - Id, userregionid
userRestriction - userId, restrictionGroup

例如,

用户Table

EID-999 | 12345
EID- 888 | 12345
D-900 | 2322
F-943 | 6767

用户限制Table

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-999F-943 分别属于 userregionId 123456767,并且他们被分配了超过 1 个限制组。

我的努力

我编写了一个查询,显示在同一 userregionID 中具有 > 1 个 restrictionGroup 的用户列表 但我不知道如何进一步将此查询转换为只能从整个数据库中获取计数和 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

您在这里可能不需要嵌套查询,下面的 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可以帮你形象化。