经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 数据库/运维 » MS SQL Server » 查看文章
SQL Server 开窗函数 Over()代替游标的使用
来源:cnblogs  作者:至情相遇  时间:2020/11/9 15:56:03  对本文有异议

前言:今天在优化工作中遇到的sql慢的问题,发现以前用了挺多游标来处理数据,这样就导致在数据量多的情况下,需要一行一行去遍历从而计算需要的数据,这样处理的结果就是数据慢,容易卡死。

语法介绍:

1、与Row_Number() 函数结合使用,对结果进行排序,这个是我们使用的非常多的

  1. SELECT ROW_NUMBER() OVER(ORDER BY FItemID DESC) FSort,* FROM Organization

  

2、与聚合函数结合使用,利用over子句的分组和排序,对需要的数据进行操作

例如:SUM() Over() 累加值、AVG() Over() 平均数
           MAX() Over() 最大值、MIN() Over() 最小值

具体介绍:

下面模拟工作中通过开窗函数代替游标的例子,通过期初余额与单据的预收金额、应收金额、实收金额来计算截止本单的期末余额,在以往就是通过游标一行一行去遍历,计算需要的期末余额,现在使用SUM() Over()来代替,最终要实现的效果图如下:

第一行表示标题;第二行表示客户,是一行空行;第三行是期初余额,只显示期末余额的数据,第四至第六行表示的是每种单据的余额情况,并逐步汇总当前行的期末余额数据;最后一行表示的是对客户的合计。

1、构建需要用到的表和数据(简略版)

  1. --客户表
  2. CREATE TABLE Organization(
  3. FItemID INT NOT NULL PRIMARY KEY IDENTITY(1,1),
  4. FNumber NVARCHAR(255),
  5. FName NVARCHAR(255)
  6. )
  7.  
  8. --期初数据表
  9. CREATE TABLE InitialData(
  10. FID INT NOT NULL PRIMARY KEY IDENTITY(1,1),
  11. FCustId INT NOT NULL,
  12. FPreAmount DECIMAL(28,10) NOT NULL DEFAULT(0), --预收金额
  13. FReceivableAmount DECIMAL(28,10) NOT NULL DEFAULT(0), --应收金额
  14. FReceiveAmount DECIMAL(28,10) NOT NULL DEFAULT(0) --实收金额
  15. )
  16.  
  17. --单据明细表
  18. CREATE TABLE DetailData(
  19. FID INT NOT NULL PRIMARY KEY IDENTITY(1,1),
  20. FCustId INT NOT NULL,
  21. FDate DATETIME NOT NULL,
  22. FBillType NVARCHAR(64) NOT NULL,
  23. FBillNo NVARCHAR(64) NOT NULL,
  24. FPreAmount DECIMAL(28,10) NOT NULL DEFAULT(0), --预收金额
  25. FReceivableAmount DECIMAL(28,10) NOT NULL DEFAULT(0), --应收金额
  26. FReceiveAmount DECIMAL(28,10) NOT NULL DEFAULT(0) --实收金额
  27. )
  28.  
  29. INSERT INTO Organization(FNumber,FName) VALUES('001','北京客户')
  30. INSERT INTO Organization(FNumber,FName) VALUES('002','上海客户')
  31. INSERT INTO Organization(FNumber,FName) VALUES('003','广州客户')
  32.  
  33. INSERT INTO InitialData(FCustId,FPreAmount,FReceivableAmount,FReceiveAmount)
  34. VALUES(1,0,0,0)
  35. INSERT INTO InitialData(FCustId,FPreAmount,FReceivableAmount,FReceiveAmount)
  36. VALUES(2,8000,7245,0)
  37. INSERT INTO InitialData(FCustId,FPreAmount,FReceivableAmount,FReceiveAmount)
  38. VALUES(3,0,1068.21,1068.00)
  39. INSERT INTO DetailData(FCustId,FDate,FBillType,FBillNo,FPreAmount,FReceivableAmount,FReceiveAmount)
  40. VALUES(1,'2020-06-30','委托结算','XSD20200700008',0,1221.56,0)
  41. INSERT INTO DetailData(FCustId,FDate,FBillType,FBillNo,FPreAmount,FReceivableAmount,FReceiveAmount)
  42. VALUES(1,'2020-06-30','委托结算','XSD20200700009',0,373.46,0)
  43. INSERT INTO DetailData(FCustId,FDate,FBillType,FBillNo,FPreAmount,FReceivableAmount,FReceiveAmount)
  44. VALUES(1,'2020-06-30','委托结算退货','XSD20200700010',0,-427.05,0)
  45. INSERT INTO DetailData(FCustId,FDate,FBillType,FBillNo,FPreAmount,FReceivableAmount,FReceiveAmount)
  46. VALUES(1,'2020-07-30','销售商品返利','XSFL20200700005',0,-17.9,0)
  47.  
  48. INSERT INTO DetailData(FCustId,FDate,FBillType,FBillNo,FPreAmount,FReceivableAmount,FReceiveAmount)
  49. VALUES(2,'2020-06-25','预收退款','SKD20200700002',-755,0,0)
  50. INSERT INTO DetailData(FCustId,FDate,FBillType,FBillNo,FPreAmount,FReceivableAmount,FReceiveAmount)
  51. VALUES(2,'2020-06-20','销售发货','XSD20200700006',0,6169.50,6169.50)
  52. INSERT INTO DetailData(FCustId,FDate,FBillType,FBillNo,FPreAmount,FReceivableAmount,FReceiveAmount)
  53. VALUES(2,'2020-07-30','销售总额返利','XSFL20200700002',0,-493.56,-421.85)
  54. INSERT INTO DetailData(FCustId,FDate,FBillType,FBillNo,FPreAmount,FReceivableAmount,FReceiveAmount)
  55. VALUES(2,'2020-07-31','其他应收','QTYS20200900001',0,6000.00,0)
  56. INSERT INTO DetailData(FCustId,FDate,FBillType,FBillNo,FPreAmount,FReceivableAmount,FReceiveAmount)
  57. VALUES(2,'2020-06-20','预收冲应收','HXD20200700006',-7245.00,0,7245.00)
  58.  
  59. INSERT INTO DetailData(FCustId,FDate,FBillType,FBillNo,FPreAmount,FReceivableAmount,FReceiveAmount)
  60. VALUES(3,'2020-06-30','销售收款','SKD20200700003',0,0,2386.96)
  61. INSERT INTO DetailData(FCustId,FDate,FBillType,FBillNo,FPreAmount,FReceivableAmount,FReceiveAmount)
  62. VALUES(3,'2020-06-30','应收转应收','HXD20200700007',0,2386.75,0)
  63. INSERT INTO DetailData(FCustId,FDate,FBillType,FBillNo,FPreAmount,FReceivableAmount,FReceiveAmount)
  64. VALUES(3,'2020-07-08','销售退货','XSD20200700014',0,-46.80,0)
  65. GO

2、以往的游标写法

  1. SET NOCOUNT ON
  2. --建立临时表处理获取数据
  3. CREATE TABLE #DATA(
  4. FID INT NOT NULL PRIMARY KEY IDENTITY(1,1),
  5. FClassTypeId INT NOT NULL,
  6. FCustId INT NOT NULL,
  7. FNumber NVARCHAR(255),
  8. FName NVARCHAR(255),
  9. FDate DATETIME NULL,
  10. FBillType NVARCHAR(64) NULL,
  11. FBillNo NVARCHAR(64) NULL,
  12. FPreAmount DECIMAL(28,10) NOT NULL DEFAULT(0), --预收金额
  13. FReceivableAmount DECIMAL(28,10) NOT NULL DEFAULT(0), --应收金额
  14. FReceiveAmount DECIMAL(28,10) NOT NULL DEFAULT(0), --实收金额
  15. FBalanceAmount DECIMAL(28,10) NOT NULL DEFAULT(0) --期末余额
  16. )
  17.  
  18. Declare @Id INT
  19. Declare @CustId INT
  20. Declare @PreAmount decimal(28,10)
  21. Declare @ReceivableAmount decimal(28,10)
  22. Declare @ReceiveAmount decimal(28,10)
  23. Declare @OldCustId int
  24. Declare @Count int
  25. Declare @LastAmount decimal(28,10)
  26. Declare @SumPreAmount decimal(28,10)
  27. Declare @SumReceivableAmount decimal(28,10)
  28. Declare @SumReceiveAmount decimal(28,10)
  29. Declare @SumBalanceAmount decimal(28,10)
  30.  
  31. --使用游标
  32. Declare Data_cursor Cursor
  33. For Select FID,FCustId,FPreAmount,FReceivableAmount,FReceiveAmount
  34. From DetailData
  35. Order By FCustId,FDate,FID
  36. OPEN Data_cursor
  37. FETCH NEXT FROM Data_Cursor INTO @Id,@CustId,@PreAmount,@ReceivableAmount,@ReceiveAmount
  38. SET @OldCustId = @CustId
  39. SET @Count = 0
  40. SET @LastAmount = 0
  41. SET @SumPreAmount = 0
  42. SET @SumReceivableAmount = 0
  43. SET @SumReceiveAmount = 0
  44. SET @SumBalanceAmount = 0
  45. WHILE @@FETCH_STATUS = 0
  46. BEGIN
  47. IF @Count > 0
  48. BEGIN
  49. IF @OldCustId <> @CustId
  50. BEGIN
  51. --表示客户已经变了,要插入小计
  52. SET @Count = 0
  53. INSERT INTO #DATA(FClassTypeId,FBillType,FCustId,FNumber,FName,FPreAmount,FReceivableAmount,FReceiveAmount,FBalanceAmount)
  54. SELECT -9999,FName + '小计',FItemID,FNumber,FName,@SumPreAmount,@SumReceivableAmount,@SumReceiveAmount,@LastAmount
  55. FROM Organization
  56. WHERE FItemID = @OldCustId
  57. Select @SumPreAmount=0,@SumReceivableAmount=0,@SumReceiveAmount=0,@SumBalanceAmount=0,@LastAmount=0
  58. END
  59. END
  60. IF @Count = 0
  61. BEGIN
  62. Set @OldCustId=@CustId
  63. --插入一行空行
  64. INSERT INTO #DATA(FClassTypeId,FBillType,FCustId,FNumber,FName)
  65. SELECT -1000,FName,FItemID,FNumber,FName
  66. FROM Organization
  67. WHERE FItemID = @CustId
  68.  
  69. --获取期初的期末余额
  70. SELECT @LastAmount=isnull(FReceivableAmount,0) - isnull(FPreAmount,0) - isnull(FReceiveAmount,0),@PreAmount=isnull(FPreAmount,0),@ReceivableAmount=isnull(FReceivableAmount,0),@ReceiveAmount=isnull(FReceiveAmount,0)
  71. FROM InitialData
  72. WHERE FCustId = @CustId
  73.  
  74. INSERT INTO #DATA(FClassTypeId,FBillType,FCustId,FNumber,FName,FBalanceAmount)
  75. VALUES(-1000,'期初余额',@CustId,'','',@LastAmount)
  76.  
  77. SELECT @Count = 1
  78. SELECT @SumBalanceAmount = @LastAmount
  79. END
  80.  
  81. --插入单据明细
  82. INSERT INTO #DATA(FClassTypeId,FCustId,FNumber,FName,FDate,FBillType,FBillNo,FPreAmount,FReceivableAmount,FReceiveAmount,FBalanceAmount)
  83. SELECT 0,d.FCustId,o.FNumber,o.FName,FDate,FBillType,FBillNo,FPreAmount,FReceivableAmount,FReceiveAmount,@LastAmount + FReceivableAmount - FPreAmount - FReceiveAmount
  84. FROM DetailData d
  85. INNER JOIN Organization o ON d.FCustId = o.FItemID
  86. WHERE d.FCustId = @CustId AND FID = @Id
  87.  
  88. SELECT
  89. @LastAmount = @LastAmount + FReceivableAmount - FPreAmount - FReceiveAmount,
  90. @SumPreAmount=@SumPreAmount + FPreAmount,@SumReceivableAmount=@SumReceivableAmount + FReceivableAmount,
  91. @SumReceiveAmount=@SumReceiveAmount + FReceiveAmount
  92. FROM DetailData
  93. WHERE FCustId = @CustId AND FID = @Id
  94.  
  95. FETCH NEXT FROM Data_cursor INTO @Id,@CustId,@PreAmount,@ReceivableAmount,@ReceiveAmount
  96. END
  97. IF @Count > 0
  98. BEGIN
  99. INSERT INTO #DATA(FClassTypeId,FBillType,FCustId,FNumber,FName,FPreAmount,FReceivableAmount,FReceiveAmount,FBalanceAmount)
  100. SELECT -9999,FName + '小计',FItemID,FNumber,FName,@SumPreAmount,@SumReceivableAmount,@SumReceiveAmount,@LastAmount
  101. FROM Organization
  102. WHERE FItemID = @OldCustId
  103. Select @SumPreAmount=0,@SumReceivableAmount=0,@SumReceiveAmount=0,@SumBalanceAmount=0,@LastAmount=0
  104. END
  105. CLOSE Data_cursor
  106. DEALLOCATE Data_cursor
  107.  
  108. SELECT * FROM #DATA
  109. ORDER BY FCustId,FID
  110.  
  111. DROP TABLE #DATA

代码说明:创建了一个临时表,使用游标遍历我们的DetailData数据表,为了呈现我们最终需要的数据样式,插入客户空行、期初余额、单据信息、客户小计等,逐行计算期末余额值的情况,最终效果如下:

3、使用SUM() Over()的写法

  1. SET NOCOUNT ON
  2. --建立临时表处理获取数据
  3. CREATE TABLE #DATA(
  4. FID INT NOT NULL PRIMARY KEY IDENTITY(1,1),
  5. FClassTypeId INT NOT NULL,
  6. FCustId INT NOT NULL,
  7. FNumber NVARCHAR(255),
  8. FName NVARCHAR(255),
  9. FDate DATETIME NULL,
  10. FBillType NVARCHAR(64) NULL,
  11. FBillNo NVARCHAR(64) NULL,
  12. FPreAmount DECIMAL(28,10) NOT NULL DEFAULT(0), --预收金额
  13. FReceivableAmount DECIMAL(28,10) NOT NULL DEFAULT(0), --应收金额
  14. FReceiveAmount DECIMAL(28,10) NOT NULL DEFAULT(0), --实收金额
  15. FBalanceAmount DECIMAL(28,10) NOT NULL DEFAULT(0) --期末余额
  16. )
  17.  
  18. --插入空行
  19. INSERT INTO #DATA(FClassTypeId,FBillType,FCustId,FNumber,FName)
  20. SELECT -1000,FName,FItemID,FNumber,FName
  21. FROM Organization o
  22. INNER JOIN (SELECT FCustId FROM DetailData GROUP BY FCustId) d ON d.FCustId = o.FItemID
  23.  
  24. --插入期初余额
  25. INSERT INTO #DATA(FClassTypeId,FBillType,FCustId,FNumber,FName,FBalanceAmount)
  26. SELECT -1000,'期初余额',FItemID,'','',i.FReceivableAmount - i.FPreAmount -i.FReceiveAmount
  27. FROM Organization o
  28. INNER JOIN InitialData i ON o.FItemID = i.FCustId
  29. INNER JOIN (SELECT FCustId FROM DetailData GROUP BY FCustId) d ON d.FCustId = o.FItemID
  30.  
  31. --插入单据明细(关键代码SUM() Over() )
  32. INSERT INTO #DATA(FClassTypeId,FCustId,FNumber,FName,FDate,FBillType,FBillNo,FPreAmount,FReceivableAmount,FReceiveAmount,FBalanceAmount)
  33. SELECT 0,d.FCustId,o.FNumber,o.FName,d.FDate,d.FBillType,d.FBillNo,d.FPreAmount,d.FReceivableAmount,d.FReceiveAmount,
  34. SUM(d.FReceivableAmount - d.FPreAmount - d.FReceiveAmount) OVER(PARTITION BY d.FCustId ORDER BY d.FCustId,d.FDate,d.FID)
  35. + i.FReceivableAmount - i.FPreAmount - i.FReceiveAmount
  36. FROM DetailData d WITH(NOLOCK)
  37. INNER JOIN Organization o WITH(NOLOCK) ON o.FItemID = d.FCustId
  38. INNER JOIN InitialData i WITH(NOLOCK) ON o.FItemID = i.FCustId
  39. ORDER BY d.FCustId,d.FDate,d.FID
  40.  
  41. --插入小计
  42. INSERT INTO #DATA(FClassTypeId,FBillType,FCustId,FNumber,FName,FPreAmount,FReceivableAmount,FReceiveAmount,FBalanceAmount)
  43. SELECT -9999,FName + '小计',d.FCustId,FNumber,FName,SUM(FPreAmount),SUM(FReceivableAmount),SUM(FReceiveAmount),0
  44. FROM dbo.DetailData d
  45. INNER JOIN dbo.Organization o ON d.FCustId = o.FItemID
  46. GROUP BY d.FCustId,o.FName,o.FNumber
  47.  
  48. --更新小计的期末余额
  49. UPDATE d SET d.FBalanceAmount = d.FReceivableAmount - d.FPreAmount - d.FReceiveAmount + i.FReceivableAmount - i.FPreAmount - i.FReceiveAmount
  50. FROM #DATA d
  51. INNER JOIN InitialData i ON d.FCustId = i.FCustId
  52. WHERE d.FClassTypeId = -9999
  53.  
  54. SELECT * FROM #DATA
  55. ORDER BY FCustId,FID
  56.  
  57. DROP TABLE #DATA

代码说明:相比第二种,去除了游标的写法,通过了

  1. SUM(d.FReceivableAmount - d.FPreAmount - d.FReceiveAmount) OVER(PARTITION BY d.FCustId ORDER BY d.FCustId,d.FDate,d.FID)

来计算我们需要的值,这个语法说明一下,sum是累加计算,计算应收金额 - 预收金额 - 实收金额(第二行计算出来的结果要加上第一行计算出来的结果,第三行计算出来的结果要加上第二行计算出来的结果,依次类推,所以,其他聚合函数也是这种用法哦),PARTITION BY分组统计客户,并通过Order by指定排序
这个PARTITION BY和Order By结果的用法就很关键了,不然计算就不是预期想要的
再举个例子:比如使用Count() Over() 计算客户的订单号

  1. SELECT DISTINCT FCustId,COUNT(FBillNo) OVER(PARTITION BY FCustId) FBillNum FROM DetailData

总结:

1、游标的使用场景可以很广,但是在数据量大的时候,就会显得很慢,一行一行遍历的速度还是挺久的

2、使用开窗函数来实现一些功能,还是很方便能实现效果,并且它的速度也是很快,值得推荐。

 

原文链接:http://www.cnblogs.com/cxt618/p/13826000.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号