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. LINUX 网络编程 原始套接字

    一 原始套接字 原始套接字(SOCK_RAW)是一种不同于SOCK_STREAM.SOCK_DGRAM的套接字,它实现于系统核心.然而,原始套接字能做什么呢?首先来说,普通的套接字无法处理ICMP.I ...

  2. 黑马程序员_<<Set,HashSet>>

    --------------------ASP.Net+Android+IOS开发..Net培训.期待与您交流! -------------------- 1.Set Set是Collection接口 ...

  3. Cocoapod安装使用

    近期换工作.项目中推荐使用Cocoapod,把经常使用的第三方库增加到project中,大致说一下流程,标记一下. 第一步:安装 CocoaPods是跑在Ruby的软件,安装可能须要几分钟.打开你的终 ...

  4. openfire研究之部署连接管理器(connection manager)

    http://blog.sina.com.cn/s/blog_7325f5150101bafh.html 一. Openfire Connection Manager 简介 Openfire Conn ...

  5. 具体总结 Hive VS 传统关系型数据库

    本文思路,看图说话,一张图,清晰总结二者差别 以下对图中的各条做具体总结 1.查询语言 不做赘述 2.数据存储位置 不做赘述 3.数据格式 Hive:Hive 中未定义专门的数据格式,数据格式能够由用 ...

  6. Java Swing界面编程(27)---JRadioButton事件处理

    在单选button操作中.能够使用ItemListener接口进行事件的监听. package com.beyole.util; import java.awt.Container; import j ...

  7. 《JavaScript 闯关记》之简介

    简介 JavaScript 是面向 Web 的编程语言,绝大多数现代网站都使用了 JavaScript,并且所有的现代 Web 浏览器(电脑,手机,平板)均包含了 JavaScript 解释器. 这使 ...

  8. asp.net mvc 生成条形码

    using System; using System.Collections; using System.Collections.Generic; using System.Drawing; usin ...

  9. iOS分类

    ios中的分类其实就是把两个类用两个或多个文件写的,,在平时的开发中我们会发现有的时候我们想要一个类多个什么功能  但是已经定义好的类中没有,,我们又不想更改我们的程序  那么现在的这种情况下我就可以 ...

  10. n多边形面积

    “ 改革春风吹满地,不会AC没关系;实在不行回老家,还有一亩三分地.谢谢!(乐队奏乐)” 话说部分学生心态极好,每天就知道游戏,这次考试如此简单的题目,也是云里雾里,而且,还竟然来这么几句打油诗.好呀 ...