SQL游标+递归查询客户子客户转换率
ALTER PROCEDURE [dbo].[Account3YearsConversion ] as
DECLARE @AccountId UNIQUEIDENTIFIER ,
@yearbeforamountA int ,
@yearbeforamountB int ,
@lastyearamountA int,
@lastyearamountB int,
@yt_purchase_amountcloseA int ,
@yt_purchase_amountcloseB int ,
@yt_forecast_amountAdvancingA int ,
@yt_purchase_amountAdvancingB int ,
@nowdate int
SELECT @nowdate=DATEPART(year,Getdate())
DECLARE mycursor CURSOR
FOR
select accountid from account WHERE StateCode=0 and yt_sap_code is not null and yt_sap_code<>'' --打开游标
OPEN mycursor
--从游标里取出数据赋值到我们刚才声明的变量中
FETCH NEXT FROM mycursor INTO @AccountId
--判断游标的状态
-- 0 fetch语句成功
---1 fetch语句失败或此行不在结果集中
---2 被提取的行不存在 WHILE ( @@fetch_status = 0 )
BEGIN
--前年转化率A
WITH childAccount(accountid,ParentAccountId) as
(
SELECT accountid,ParentAccountId FROM AccountBase WHERE accountid=@AccountId
UNION ALL
SELECT A.accountid,A.ParentAccountId FROM AccountBase A,childAccount b
where a.ParentAccountId = b.accountid
)
select
@yearbeforamountA=(select ISNULL(sum(yt_purchase_amount),0) from Opportunity a join childAccount b on a.yt_arrive=b.accountid
where DATEPART(year,DATEADD(MM,-2,a.yt_invoice_date))=@nowdate-2 and a.yt_status=100000002),
@yearbeforamountB=(select ISNULL(sum(yt_purchase_amount),0) from Opportunity a join childAccount b on a.yt_arrive=b.accountid
where DATEPART(year,DATEADD(MM,-2,a.yt_require_date))=@nowdate-2 and
(a.yt_status=100000002 or a.yt_status=100000003 or (a.yt_status=100000001 and a.yt_improve_winrate2=100000001))),
@lastyearamountA=(select ISNULL(sum(yt_purchase_amount),0) from Opportunity a join childAccount b on a.yt_arrive=b.accountid
where DATEPART(year,DATEADD(MM,-2,yt_invoice_date))=@nowdate-1 and a.yt_status=100000002),
@lastyearamountB=(select ISNULL(sum(yt_purchase_amount),0) from Opportunity a join childAccount b on a.yt_arrive=b.accountid
where DATEPART(year,DATEADD(MM,-2,a.yt_require_date))=@nowdate-1 and
(a.yt_status=100000002 or a.yt_status=100000003 or (a.yt_status=100000001 and a.yt_improve_winrate2=100000001))),
@yt_purchase_amountcloseA=(select ISNULL(sum(yt_purchase_amount),0) from Opportunity a join childAccount b on a.yt_arrive=b.accountid
where DATEPART(year,DATEADD(MM,-2,a.yt_require_date))=@nowdate
and (a.yt_status=100000002 or (a.yt_status=100000001 and a.yt_improve_winrate2=100000001))),
@yt_purchase_amountcloseB=(select ISNULL(sum(yt_purchase_amount),0) from Opportunity a join childAccount b on a.yt_arrive=b.accountid
where DATEPART(year,DATEADD(MM,-2,a.yt_require_date))=@nowdate
and (a.yt_status=100000002 or a.yt_status=100000003 or (a.yt_status=100000001 and a.yt_improve_winrate2=100000001))),
@yt_forecast_amountAdvancingA=(select ISNULL(sum(yt_forecast_amount),0) from Opportunity a join childAccount b on a.yt_arrive=b.accountid
where DATEPART(year,DATEADD(MM,-2,a.yt_require_date))=@nowdate
and (a.yt_status=100000001 and a.yt_improve_winrate2!=100000001)),
@yt_purchase_amountAdvancingB=(select ISNULL(sum(yt_purchase_amount),0) from Opportunity a join childAccount b on a.yt_arrive=b.accountid
where DATEPART(year,DATEADD(MM,-2,a.yt_require_date))=@nowdate
and (a.yt_status=100000001 and a.yt_improve_winrate2!=100000001)) update account
set yt_yearbeforconversion = Case @yearbeforamountB when 0
then null
else
ltrim(Convert(numeric(9,2),@yearbeforamountA*100.0/@yearbeforamountB))+'%'
end,
yt_lastyearconversion = case @lastyearamountB when 0
then null
else
ltrim(convert(numeric(9,2),@lastyearamountA*100.0/@lastyearamountB))+'%'
end,
yt_thisyearcloseconversion = case @yt_purchase_amountcloseB when 0
then null
else
ltrim(convert(numeric(9,2),@yt_purchase_amountcloseA*100.0/@yt_purchase_amountcloseB))+'%'
end,
yt_thisyearadvancingconversion = case @yt_purchase_amountAdvancingB when 0
then null
else
ltrim(convert(numeric(9,2),@yt_forecast_amountAdvancingA*100.0/@yt_purchase_amountAdvancingB))+'%'
end,
yt_thisyearpredictconversion = case @yt_purchase_amountcloseB+@yt_purchase_amountAdvancingB when 0
then null
else
ltrim(convert(numeric(9,2),(@yt_purchase_amountcloseA+@yt_forecast_amountAdvancingA)*100.0/(@yt_purchase_amountcloseB+@yt_purchase_amountAdvancingB)))+'%'
end
where AccountId =@AccountId --用游标去取下一条客户
FETCH NEXT FROM mycursor INTO @AccountId
END
--关闭游标
CLOSE mycursor
--撤销游标
DEALLOCATE mycursor
SQL游标+递归查询客户子客户转换率的更多相关文章
- Oracle SQL 硬解析和子游标
Oracle SQL 硬解析和子游标 What reasons will be happening sql hard parse and generating new child cursors 在一 ...
- 网上看到一份详细sql游标说明 《转载 https://www.cnblogs.com/xiongzaiqiren/p/sql-cursor.html》
SQL游标(cursor)详细说明及内部循环使用示例 游标 游标(cursor)是系统为用户开设的一个数据缓冲区,存放SQL语句的执行结果.每个游标区都有一个名字,用户可以用SQL语句逐一从游标中获 ...
- Library Cache优化与SQL游标
Library Cache主要用于存放SQL游标,而SQL游标最大化共享是Library Cache优化的重要途径,可以使SQL运行开销最低.性能最优. 1 SQL语句与父游标及子游标 在PL/SQL ...
- SQL Server游标 C# DataTable.Select() 筛选数据 什么是SQL游标? SQL Server数据类型转换方法 LinQ是什么? SQL Server 分页方法汇总
SQL Server游标 转载自:http://www.cnblogs.com/knowledgesea/p/3699851.html. 什么是游标 结果集,结果集就是select查询之后返回的所 ...
- sql 游标例子 根据一表的数据去筛选另一表的数据
sql 游标例子 根据一表的数据去筛选另一表的数据 DECLARE @MID nvarchar(20)DECLARE @UTime datetime DECLARE @TBL_Temp table( ...
- sql 游标循环当中重新赋值
sql 游标循环当中的变量必须重新赋值不然变量的值就是前次循环的值
- MS SQL Server递归查询
原文:MS SQL Server递归查询 刚才在论坛上看到网友一个要求.参考如下,Insus.NET分析一下,可以使用MS SQL Server的递归查询,得到结果.准备一张表: 根据网友提供的数据, ...
- SQL 游标的应用
----------------SQL游标应用-----------------if object_id('tempdb..#test0001') is not null drop table #te ...
- PL/SQL 游标 (实验七)
PL/SQL 游标 emp.dept 目标表结构及数据 要求 基于部门表建立游标dept_cursor1,使用记录变量接收游标数据,输出部门表信息: 显示格式: 部 门 号: XXX 部门名称: XX ...
随机推荐
- mysql insert中文乱码无法插入ERROR 1366 (HY000): Incorrect string value
ERROR 1366 (HY000): Incorrect string value: '\xB1\xEA\xCC\xE2\xD5\xE2...' for column 'title' at row ...
- 附加导航(Affix)行为
用法 通过 data 属性:如需向元素添加附加导航(Affix)行为,只需要向需要监听的元素添加 data-spy="affix" 即可.请使用偏移来定义何时切换元素的锁定和移动. ...
- JavaScript里面三个等号和两个等号的区别
== equality 等同,=== identity 恒等. ==, 两边值类型不同的时候,要先进行类型转换,再比较. ===,不做类型转换,类型不同的一定不等. 下面分别说明: 先说 ===,这个 ...
- Ubuntu14.04如何备份和恢复系统
清理Ubuntu14.04的系统的垃圾:先清空回收站,软件升级到最新.Ubuntu系统与Windows系统所采用的文件系统不同, Ubuntu系统在使用或更新过程中不会产生文件碎片和垃圾文件,所以在使 ...
- 菜鸟的jQuery源码学习笔记(三)
each: function(callback, args) { return jQuery.each(this, callback, args); }, each:这个调用了jQuery.each方 ...
- Android之 compileSdkVersion, minSdkVersion, and targetSdkVersion
在一年之中,也有可能只是几个月后,你会发布你的Android应用程序.但是这中间有可能会发布新的版本的Android系统,比如:6.0+. 你肯定希望你的应用可以向前兼容,当用户升级到新的Androi ...
- ApiGen4.1 windows安装教程
一. ApiGen4.1版本介绍 1.ApiGen介绍 ApiGen是自动生成PHP项目的阅读文档工具. 用于从PHP源代码创建专业的API文档,类似于phpDocumentor/phpDoc. Ap ...
- Linux 下搭建ftp服务器 指定用户指定目录及其他操作
搭建 Linux下 rpm -qa |grep vsftpd查看是否安装 没安装yum安装 /etc/vsftpd/目录下有vsftpd.conf配置文件 根据需求 进行配置 是否使用匿名用户以及文 ...
- laravel5.1框架简介及安装
最近自己出来实习了,进入了一个新的环境,不仅是生活中,在代码和架构中也完全是一个新的架构.由于公司使用laravel5.1框架,所以最近学习了laravel5.1框架,好了接下来就简单介绍一下lara ...
- 转:JavaScript函数式编程(一)
转:JavaScript函数式编程(一) 一.引言 说到函数式编程,大家可能第一印象都是学院派的那些晦涩难懂的代码,充满了一大堆抽象的不知所云的符号,似乎只有大学里的计算机教授才会使用这些东西.在曾经 ...