请教一个存储过程的写法?(100分)

bluefk 2003-05-16 04:09:27
现在有表如下字段traindata
clusterid //类号
customerid //顾客号
trainid //训练号
churnflag //流失标志 0 流失,1 不流失
每个trainid对应多条记录
现在要统记每次训练(根据trainid)中,某一类(根据clusterid)的顾客总数customernum,和这类顾客的流失率churnrate(a类流失顾客数/a类顾客的总数)并写入表trainresult中,字段如下
clusterid
customernum //顾客数量
churnrate //流失率

希望那位高手给写一下,不胜感激!
...全文
25 点赞 收藏 6
写回复
6 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
erigido 2003-05-18
up
回复
dlkfth 2003-05-16
来晚拉
回复
shierre 2003-05-16
to tj_dns(愉快的登山者),
你这个1 - sum(cast(churnflag as int))会不会有问题?
回复
create PROCEDURE pro_test @clusterid int, @trainid int
as
insert into trainresult
select @clusterid, count(clusterid), 1 - sum(cast(churnflag as int))/count(clusterid)
from traindata where clusterid =@clusterid and trainid=@trainid

回复
funboy88 2003-05-16
insert into trainresult (trainid,clusterid,customernum,churnrate) (select trainid,clusterid,count(customerid),sum(case churnflag when 0 then 1 else 0 end))/count(customerid) from traindata group by trainid, clusterid)
回复
caiyunxia 2003-05-16

create PROCEDURE pro_test @clusterid int,@trainid int
as
insert into trainresult
select @clusterid,count(*),1.0*sum(case when churnflag=0 then 1 else 0 end)/count(*)
from traindata where clusterid =@clusterid and trainid=@trainid
回复
相关推荐
发帖
MS-SQL Server
创建于2007-09-28

3.3w+

社区成员

MS-SQL Server相关内容讨论专区
申请成为版主
帖子事件
创建了帖子
2003-05-16 04:09
社区公告
暂无公告