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. OpenStack Block Storage安装配置use LVM

    1,storage systems use LVM Ins and configuration Block Storage; apt-get install lvm2; 创建Physical volu ...

  2. python 重要模块

    1,使用字典的特殊字符串替换,基于字典的字符串格式化

  3. Android4.0中蓝牙适配器state machine(状态机)的分析

    今天晓东和大家来一起看一下Android4.0中蓝牙适配器(Bluetooth Adapter)的状态机变化的过程.首先,我们需要了解一下,蓝牙适配器究竟有哪些状态,从代码可以清晰地看到(framew ...

  4. Swift String 一些常用方法

    //字符串 //1 判断字符串是否为空 var test1Str="" var test1Str2:String = String(); println("test1St ...

  5. Linux下如何进行FTP安装与设置

    1. 先用rpm -qa| grep vsftpd命令检查是否已经安装,如果ftp没有安装,使用yum  -y  install vsftpd 安装,(ubuntu 下使用apt-get instal ...

  6. Asp.net MVC + EF6.0 经常出现的问题

    1.运行视图时出现问题:未能加载文件或程序集"EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c ...

  7. 手机不支持onchange事件

    今天,微信上用input type=date来计算日期,苹果上可以,我的小米2s手机死活不触发onchange,大三星的onchange效果怎么看都是onblur.听燕哥说,这事儿,得折腾. 搞来搞去 ...

  8. 利用sql里的xpath生成表格

    SELECT (SELECT * from Common.Common_Setup_CityData FOR XML PATH('tr'), TYPE).query('      for $item ...

  9. hdu1004Let the Balloon Rise

    Problem Description Contest time again! How excited it is to see balloons floating around. But to te ...

  10. Android 原生listview item伸展收缩效果 (续)

    接上一个原生的listview item的伸展收缩效果. 上一个可能做的有些粗糙,效果也没有这个好,上代码. package com.example.listviewdemo; import java ...