[英]Oracle SQL query using partition
我有這個查詢:
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 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 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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.