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 ...
随机推荐
- 基于winform的二进制图片数据的存取(用于数据库照片的读写处理)
编程目的:文本框1中输入id号,则从openFileDialog中选择的图片会以二进制数据存进SQL数据库的对应表的id列:文本框2中输入姓名,从数据库读取对应name的照片并显示在pictureBo ...
- svm中的数学和算法
支持向量机(Support Vector Machine)是Cortes和Vapnik于1995年首先提出的,它在解决小样本.非线性及高维模式识别中表现出很多特有的优势,并可以推广应用到函数拟合等其它 ...
- mma ctf 1st && csaw 2015
(很久以前做的,现在发一下)最近做了两个CTF,水平太渣,做了没几道题,挑几个自己做的记录一下. mma ctf 1st 之 rps: from socket import * s = socket( ...
- CSS基础知识之position
最近在慕课网学习了 网页布局基础 和 固定层效果 ,都是由声音甜美的 婧享人生 老师所录制,视频详细讲解了CSS中position的用法,在此把学习笔记分享给大家. CSS定位机制 标准文档流(Nor ...
- linux 命令入门
1 linux 中,一切皆文件. 图片.MP3和视频,它们都是文件. 目录,是一种特殊的文件,其中包含其他文件的信息.磁盘驱动器则是真正的大文件了. 网络连接也是文件,甚至运行中的进程都是文件.这些都 ...
- hibernate 之 HQL语句总结【转】
1. 查询整个映射对象所有字段 //直接from查询出来的是一个映射对象,即:查询整个映射对象所有字段 String hql = "from Users"; Query query ...
- Java学习—— for循环
For双重循环 /* 循环语句嵌套 */ class ForForTest { public static void main(String[] args) { /*int x,y = 0; for( ...
- iOS 9适配技巧(更新版)
转自: http://www.cocoachina.com/ios/20150929/13598.html 中文快速导航: 1.iOS9网络适配_ATS:改用更安全的HTTPS(见Demo1) i ...
- Visual C++ 64bit应用程序项目设置
Visual Studio 2005 This topic describes how to set up C++ applications to target 64-bit platforms us ...
- 兼容IE6,IE7和firefox可以使用的一些css hack:
.一些问题是浏览器自身的问题,遇到问题发生无法避免的情况下,那就要考虑使用一些css hack了,以下是针对IE6,IE7和firefox可以使用的一些css hack:(1) a: 针对区别IE6 ...