大家好,我似乎没有解决我的问题,我需要列出客户端ID以及客户端推荐的客户端数量...我的表格如下:
clientID Name Referred
10001 jhon null
10002 scott 10001
10003 james null
10004 nelson 10003
10005 chris 10003
10006 jack 10001
10007 jeremy null
10008 cliford 10002
10009 matt null
10010 justin 10004
10011 brad 10001
10012 jason 10008
答案 0 :(得分:1)
如果你只需要clientId
给任何有推介的人,这是一个简单的group by
:
select referred as clientId, count(*) as cnt
from t
where referred is not null
group by referred;
答案 1 :(得分:0)
通过'refer'列聚合数据并将结果与完整表连接(假设'clientdata'为表名):
Select c.clientID
, nvl(r.cnt, 0)
From clientdata c
Left join (
Select referred
, count(*) cnt
From clientdata
Group by referred
) r
On (r.referred = c.clientid)
;