[英]Oracle perform a count for each row on a table
I have a doubt regarding a query in oracle SQL. I have a groups table, that I can query as:我对 oracle SQL 中的查询有疑问。我有一个组表,我可以查询为:
SELECT *
FROM groups g
WHERE g.owner = 123;
This would give something like:这会给出类似的东西:
groupId群组ID | owner所有者 |
---|---|
1 1个 | 123 123 |
2 2个 | 123 123 |
3 3个 | 123 123 |
4 4个 | 123 123 |
I also have another table of administrators, that I can query as:我还有另一个管理员表,我可以查询为:
SELECT *
FROM admins a
ORDER BY groupId;
This would get administrators as:这将使管理员成为:
adminId adminId | userName用户名 | groupId群组ID |
---|---|---|
1 1个 | myadmin1 myadmin1 | 1 1个 |
2 2个 | myAdmin2我的管理员2 | 1 1个 |
3 3个 | myAdmin3我的管理员3 | 1 1个 |
4 4个 | myAdmin4我的管理员4 | 2 2个 |
5 5个 | myAdmin5我的管理员5 | 3 3个 |
6 6个 | myAdmin6我的管理员6 | 3 3个 |
That basically means that a group can have multiple administrators.这基本上意味着一个组可以有多个管理员。 I would like to count the number of administrators for each group.我想统计每个组的管理员人数。 A result such as:结果如:
groupId群组ID | owner所有者 | adminCount管理计数 |
---|---|---|
1 1个 | 123 123 | 3 3个 |
2 2个 | 123 123 | 1 1个 |
3 3个 | 123 123 | 2 2个 |
4 4个 | 123 123 | 0 0 |
However, I cannot make a count of each administrator in the table and then make a join, as it is a table with a lot of rows.但是,我无法对表中的每个管理员进行计数,然后进行连接,因为它是一个包含很多行的表。 I would like to perform the count query我想执行计数查询
SELECT count(*)
FROM admins a
WHERE groupId = 1;
for each row of the groups query, such that I get the desired result without performing a count of each administrator in the table, just the ones that belong to the groups from a specific owner.对于组查询的每一行,这样我就可以得到所需的结果,而无需对表中的每个管理员进行计数,只计算属于特定所有者的组的管理员。
Does someone know how can I count it without counting all the rows in the administrators table?有人知道如何在不计算管理员表中所有行的情况下计算它吗?
Thanks谢谢
The easiest and most readable variant is to use outer apply (or lateral(+)):最简单和最易读的变体是使用外部应用(或横向(+)):
select *
from groups g
outer apply (
select count(*) as adminCount
from admins a
where a.groupId=g.groupId
);
Or you can get the same results using subqueries (moreover, in fact Oracle optimizer can decide to transform outer-apply/lateral to this variant, since it has "lateral view decorrelation" transformation):或者您可以使用子查询获得相同的结果(此外,实际上 Oracle 优化器可以决定将外部应用/横向转换为此变体,因为它具有“横向视图去相关”转换):
select g.groupId,g.owner, nvl(a.adminCount,0) as adminCount
from groups g
left join (
select x.groupId, count(*) as adminCount
from admins x
group by x.groupId
) a
on a.groupId=g.groupId;
or even group-by with join:甚至通过加入进行分组:
select g.groupId,g.owner, count(a.groupId) as adminCount
from groups g
left join admins a
on g.groupId=a.groupId
group by g.groupId,g.owner
You could use analytic function COUNT() OVER()...您可以使用解析 function COUNT() OVER()...
Select Distinct
g.GROUP_ID,
g.OWNER,
Count(a.ADMIN_ID) OVER(Partition By g.GROUP_ID) "COUNT_ADMINS"
From groups g
Left Join admins a ON(a.GROUP_ID = g.GROUP_ID)
Where g.OWNER = 123
Order By g.GROUP_ID
... this requires the Distinct keyword which could be performance costly with big datasets. ...这需要 Distinct 关键字,这对于大数据集来说可能会提高性能。 I don't expect that user groups and admins are that big.我不希望用户组和管理员有那么大。
WIth your sample data:使用您的示例数据:
WITH
groups (GROUP_ID, OWNER) AS
(
Select 1, 123 From Dual Union ALL
Select 2, 123 From Dual Union ALL
Select 3, 123 From Dual Union ALL
Select 4, 123 From Dual
),
admins (ADMIN_ID, ADMIN_USER_NAMAE, GROUP_ID) AS
(
Select 1, 'myadmin1', 1 From Dual Union All
Select 2, 'myadmin2', 1 From Dual Union All
Select 3, 'myadmin3', 1 From Dual Union All
Select 4, 'myadmin4', 2 From Dual Union All
Select 5, 'myadmin5', 3 From Dual Union All
Select 6, 'myadmin6', 3 From Dual
)
... the result is ...结果是
GROUP_ID群组编号 | OWNER所有者 | COUNT_ADMINS COUNT_ADMINS 个 |
---|---|---|
1 1个 | 123 123 | 3 3个 |
2 2个 | 123 123 | 1 1个 |
3 3个 | 123 123 | 2 2个 |
4 4个 | 123 123 | 0 0 |
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.