使用分区的oraclesql查询
我有一个疑问:使用分区的oraclesql查询,sql,oracle,Sql,Oracle,我有一个疑问: select a.name, count(distinct numClient) from a group by a.name; 我想添加另一列,每行将计算前面几行的总和: JONES 3 0 SMITH 5 3 JOHN 10 8 ..... KEN 12 365 你能帮忙吗?。我想我必须使用分区,但我不太理解它。假设有一列指定计算累计和的顺序,您可以使用 select name,cntClient,
select a.name, count(distinct numClient)
from a
group by a.name;
我想添加另一列,每行将计算前面几行的总和:
JONES 3 0
SMITH 5 3
JOHN 10 8
.....
KEN 12 365
你能帮忙吗?。我想我必须使用分区,但我不太理解它。假设有一列指定计算累计和的顺序,您可以使用
select name,cntClient,sum(cntClient) over(order by orderCol rows between unbounded preceding and 1 preceding) as cumCntClient
from (select a.name, count(distinct numClient) as cntClient
from a
group by a.name
) t
假设有一列指定计算累计和的顺序,则可以使用
select name,cntClient,sum(cntClient) over(order by orderCol rows between unbounded preceding and 1 preceding) as cumCntClient
from (select a.name, count(distinct numClient) as cntClient
from a
group by a.name
) t
您根本不需要子查询:
select a.name, count(distinct numClient),
(sum(count(distinct numClient)) over (order by count(distinct numClient)) -
count(distinct numClient)
) as running_sum
from a
group by a.name;
您根本不需要子查询:
select a.name, count(distinct numClient),
(sum(count(distinct numClient)) over (order by count(distinct numClient)) -
count(distinct numClient)
) as running_sum
from a
group by a.name;
您是否有一列指定顺序?您是否有一列指定顺序?这里的orderCol是什么?计算累计和的顺序..可以是
cntClient
,根据示例显示这里的orderCol是什么?计算累计和的顺序..根据所示示例可以是cntClient