请教一个存储过程的写法?(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 //流失率

希望那位高手给写一下,不胜感激!
...全文
64 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
erigido 2003-05-18
  • 打赏
  • 举报
回复
up
dlkfth 2003-05-16
  • 打赏
  • 举报
回复
来晚拉
shierre 2003-05-16
  • 打赏
  • 举报
回复
to tj_dns(愉快的登山者),
你这个1 - sum(cast(churnflag as int))会不会有问题?
愉快的登山者 2003-05-16
  • 打赏
  • 举报
回复
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

34,838

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧