现有表:客户交易表(khjy)如下:
客户经理 客户帐号 客户姓名 交易量 手续费
北京经理 1032 客户A 256000 256
南京经理 1033 客户AC 5200 5
北京经理 1025 客户B 15000 20
北京经理 2506 客户BX 160000 320
南京经理 2036 客户M 520035 1535
广州经理 2034 客户NV 86000 200
… … … … …现在要得到如下结果:以下是列字段名客户经理 交易量<=10000的客户数量 交易量<=10000的交易量之和 10000<交易量<=200000的客户数量 10000<交易量<=200000的交易量之和
请问如何写sql代码,统计出相应结果。
客户经理 客户帐号 客户姓名 交易量 手续费
北京经理 1032 客户A 256000 256
南京经理 1033 客户AC 5200 5
北京经理 1025 客户B 15000 20
北京经理 2506 客户BX 160000 320
南京经理 2036 客户M 520035 1535
广州经理 2034 客户NV 86000 200
… … … … …现在要得到如下结果:以下是列字段名客户经理 交易量<=10000的客户数量 交易量<=10000的交易量之和 10000<交易量<=200000的客户数量 10000<交易量<=200000的交易量之和
请问如何写sql代码,统计出相应结果。
(select count(*) from tb where 客户经理=a.客户经理 and 交易量<=10000)as [交易量<=10000的客户数量],
(select sum(交易量) from tb where 客户经理=a.客户经理 and 交易量<=10000)as [交易量<=10000的交易量之和],
(select count(*) from tb where 客户经理=a.客户经理 and 交易量>10000 and 交易量<=20000)as [ 10000<交易量<=200000的客户数量],
(select sum(交易量) from tb where 客户经理=a.客户经理 and 交易量>10000 and 交易量<=20000)as [10000<交易量<=200000的交易量之和]
from tb a
客户经理,
sum(case when 交易量<=10000 then 1 else 0 end) as [交易量<=10000的客户数量],
sum(case when 交易量<=10000 then 交易量 else 0 end) as [交易量<=10000的交易量之和],
sum(case when 交易量>10000 and 交易量<=20000 then 1 else 0 end) as [10000<交易量<=200000的客户数量],
sum(case when 交易量>10000 and 交易量<=20000 then 交易量 else 0 end) as [10000<交易量<=200000的客户数量]
from
tb
group by
客户经理
;with c1 as(
select 客户经理,count(*)c,sum(交易量)s from khjy where 交易量<=10000
),c2 as(
select 客户经理,count(*)c,sum(交易量)s from khjy where 交易量>10000 and 交易量<=20000
),c3 as(
select 客户经理 from c1
union all
select 客户经理 from c2
),c4 as(
select distinct 客户经理 from c3
)select a.客户经理,b.c as [ 交易量<=10000的客户数量],b.s as [交易量<=10000的交易量之和],
c.c as [10000<交易量<=200000的客户数量],c.s as [10000<交易量<=200000的交易量之和]
from c4 a left join c1 b on a.客户经理=b.客户经理
left join c2 c on a.客户经理=c.客户经理
select 客户经理,
[交易量<=10000的客户数量]=sum(case when 交易量<=10000 then 1 else 0 end),
[交易量<=10000的交易量之和]=sum(case when 交易量<=10000 then 交易量 else 0 end),
[10000<交易量<=200000的客户数量]=sum(case when 交易量>10000 and 交易量<=200000 then 1 else 0 end),
[10000<交易量<=200000的交易量之和]=sum(case when 交易量>10000 and 交易量<=200000 then 交易量 else 0 end)
from tb group by 客户经理
;with c1 as(
select 客户经理,count(*)c,sum(交易量)s from khjy where 交易量<=10000 group by 客户经理
),c2 as(
select 客户经理,count(*)c,sum(交易量)s from khjy where 交易量>10000 and 交易量<=20000 group by 客户经理
),c3 as(
select 客户经理 from c1
union all
select 客户经理 from c2
),c4 as(
select distinct 客户经理 from c3
)select a.客户经理,b.c as [ 交易量<=10000的客户数量],b.s as [交易量<=10000的交易量之和],
c.c as [10000<交易量<=200000的客户数量],c.s as [10000<交易量<=200000的交易量之和]
from c4 a left join c1 b on a.客户经理=b.客户经理
left join c2 c on a.客户经理=c.客户经理效率没有2楼高.
create table #khjl(
客户经理 nvarchar(50),
客户帐号 nvarchar(50),
客户姓名 nvarchar(50),
交易量 int,
手续费 int
)select 客户经理
,(select count(distinct a.客户姓名) from #khjl a where a.客户经理=#khjl.客户经理 and a.交易量<=10000)
,(select sum(a.交易量) from #khjl a where a.客户经理=#khjl.客户经理 and a.交易量<=10000)
,(select count(distinct a.客户姓名) from #khjl a where a.客户经理=#khjl.客户经理 and a.交易量>10000 and a.交易量<=20000)
,(select sum(a.交易量) from #khjl a where a.客户经理=#khjl.客户经理 and a.交易量>10000 and a.交易量<=20000)
from #khjl
group by 客户经理
create table #khjl(
客户经理 nvarchar(50),
客户帐号 nvarchar(50),
客户姓名 nvarchar(50),
交易量 int,
手续费 int
)select 客户经理,
(select count(distinct a.客户姓名) from #khjl a where a.客户经理=#khjl.客户经理 and a.交易量<=10000) as [交易量<=10000的客户数量]
,(select sum(a.交易量) from #khjl a where a.客户经理=#khjl.客户经理 and a.交易量<=10000) as [交易量<=10000的交易量之和]
,(select count(distinct a.客户姓名) from #khjl a where a.客户经理=#khjl.客户经理 and a.交易量>10000 and a.交易量<=20000) as [10000<交易量<=200000的客户数量]
,(select sum(a.交易量) from #khjl a where a.客户经理=#khjl.客户经理 and a.交易量>10000 and a.交易量<=20000) as [10000<交易量<=200000的交易量之和]
from #khjl
group by 客户经理