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游标+递归查询客户子客户转换率的更多相关文章

  1. Oracle SQL 硬解析和子游标

    Oracle SQL 硬解析和子游标 What reasons will be happening sql hard parse and generating new child cursors 在一 ...

  2. 网上看到一份详细sql游标说明 《转载 https://www.cnblogs.com/xiongzaiqiren/p/sql-cursor.html》

     SQL游标(cursor)详细说明及内部循环使用示例 游标 游标(cursor)是系统为用户开设的一个数据缓冲区,存放SQL语句的执行结果.每个游标区都有一个名字,用户可以用SQL语句逐一从游标中获 ...

  3. Library Cache优化与SQL游标

    Library Cache主要用于存放SQL游标,而SQL游标最大化共享是Library Cache优化的重要途径,可以使SQL运行开销最低.性能最优. 1 SQL语句与父游标及子游标 在PL/SQL ...

  4. SQL Server游标 C# DataTable.Select() 筛选数据 什么是SQL游标? SQL Server数据类型转换方法 LinQ是什么? SQL Server 分页方法汇总

    SQL Server游标   转载自:http://www.cnblogs.com/knowledgesea/p/3699851.html. 什么是游标 结果集,结果集就是select查询之后返回的所 ...

  5. sql 游标例子 根据一表的数据去筛选另一表的数据

    sql 游标例子 根据一表的数据去筛选另一表的数据 DECLARE @MID nvarchar(20)DECLARE @UTime datetime DECLARE @TBL_Temp table( ...

  6. sql 游标循环当中重新赋值

    sql 游标循环当中的变量必须重新赋值不然变量的值就是前次循环的值

  7. MS SQL Server递归查询

    原文:MS SQL Server递归查询 刚才在论坛上看到网友一个要求.参考如下,Insus.NET分析一下,可以使用MS SQL Server的递归查询,得到结果.准备一张表: 根据网友提供的数据, ...

  8. SQL 游标的应用

    ----------------SQL游标应用-----------------if object_id('tempdb..#test0001') is not null drop table #te ...

  9. PL/SQL 游标 (实验七)

    PL/SQL 游标 emp.dept 目标表结构及数据 要求 基于部门表建立游标dept_cursor1,使用记录变量接收游标数据,输出部门表信息: 显示格式: 部 门 号: XXX 部门名称: XX ...

随机推荐

  1. OC基础8:分类和协议

    "OC基础"这个分类的文章是我在自学Stephen G.Kochan的<Objective-C程序设计第6版>过程中的笔记. 1.关于分类(category): (1) ...

  2. public void Delete<T>(List<T> EntityList) where T : class, new() 这是什么意思

    就是说T必须是一个类(class)类型,不能是结构(structure)类型. 这是类型参数约束,.NET支持的类型参数约束有以下五种: where T : struct                ...

  3. C# 获取网站的 IIS 站点名称 ,获取站点当前连接数

    System.Web.Hosting.HostingEnvironment.ApplicationHost.GetSiteName(); System.Management.ManagementObj ...

  4. Codeforces #245(div2)

    A:A. Points and Segments (easy) 题目看了n久,開始认为尼玛这是div2的题目么,题目还标明了easy.. 意思是给你一n个点,m个区间,在n个点上放蓝球或者红球,然后让 ...

  5. arm+linux 裸机环境搭建之安装工具篇(eclipse)

    之前已经讲述如何安装gcc和gdb,在此不赘述! 一.所需要的软件有两个: jre-7u25-linux-i586.rpm(虚拟机) eclipse-cpp-kepler-R-linux-gtk .t ...

  6. C# Web对文件的管理

    /// <summary> /// 创建新文件 /// </summary> /// <param name="parentPath">文件路径 ...

  7. DoNet开源项目-基于Amaze UI的点餐系统

    帮朋友做的点餐系统,主要是为了让顾客在餐桌上,使用微信扫描二维码,就可以直接点菜,吃完使用微信付款. 系统演示地址,账户名和密码均为:admin.(请不要删除admin用户) GitHub Clone ...

  8. js 正则表达式验证 整理

    1.验证首字符是英文字母: var str="123"; var reg=/^[a-zA-Z]/; if(!reg.test(str)){ alert(str+"应以字母 ...

  9. asp.net uploadfile 上传文件,连接已重置问题

    修改web.config中的配置 <httpRuntime maxRequestLength="/> //设置上传文件大小(kb)和响应时间(s) 针对iis7或更高版本另需要在 ...

  10. 【Android】Fragment如何获取子Fragment

    今天搞了个嵌套的Fragment,通过外部的Fragment获取的子Fragment代码: this.navigationBar = (HXKJCargoNavigationView) getFrag ...