Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/oracle/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用分区的oraclesql查询_Sql_Oracle - Fatal编程技术网

使用分区的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