SQL Server 开窗函数 Over()代替游标的使用详解
前言:
今天在优化工作中遇到的sql慢的问题,发现以前用了挺多游标来处理数据,这样就导致在数据量多的情况下,需要一行一行去遍历从而计算需要的数据,这样处理的结果就是数据慢,容易卡死。
语法介绍:
1、与row_number() 函数结合使用,对结果进行排序,这个是我们使用的非常多的
2、与聚合函数结合使用,利用over子句的分组和排序,对需要的数据进行操作
例如:sum() over() 累加值、avg() over() 平均数
max() over() 最大值、min() over() 最小值
具体介绍:
下面模拟工作中通过开窗函数代替游标的例子,通过期初余额与单据的预收金额、应收金额、实收金额来计算截止本单的期末余额,在以往就是通过游标一行一行去遍历,计算需要的期末余额,现在使用sum() over()来代替,最终要实现的效果图如下:
第一行表示标题;第二行表示客户,是一行空行;第三行是期初余额,只显示期末余额的数据,第四至第六行表示的是每种单据的余额情况,并逐步汇总当前行的期末余额数据;最后一行表示的是对客户的合计。
1、构建需要用到的表和数据(简略版)
--客户表 create table organization( fitemid int not null primary key identity(1,1), fnumber nvarchar(255), fname nvarchar(255) ) --期初数据表 create table initialdata( fid int not null primary key identity(1,1), fcustid int not null, fpreamount decimal(28,10) not null default(0), --预收金额 freceivableamount decimal(28,10) not null default(0), --应收金额 freceiveamount decimal(28,10) not null default(0) --实收金额 ) --单据明细表 create table detaildata( fid int not null primary key identity(1,1), fcustid int not null, fdate datetime not null, fbilltype nvarchar(64) not null, fbillno nvarchar(64) not null, fpreamount decimal(28,10) not null default(0), --预收金额 freceivableamount decimal(28,10) not null default(0), --应收金额 freceiveamount decimal(28,10) not null default(0) --实收金额 ) insert into organization(fnumber,fname) values('001','北京客户') insert into organization(fnumber,fname) values('002','上海客户') insert into organization(fnumber,fname) values('003','广州客户') insert into initialdata(fcustid,fpreamount,freceivableamount,freceiveamount) values(1,0,0,0) insert into initialdata(fcustid,fpreamount,freceivableamount,freceiveamount) values(2,8000,7245,0) insert into initialdata(fcustid,fpreamount,freceivableamount,freceiveamount) values(3,0,1068.21,1068.00) insert into detaildata(fcustid,fdate,fbilltype,fbillno,fpreamount,freceivableamount,freceiveamount) values(1,'2020-06-30','委托结算','xsd20200700008',0,1221.56,0) insert into detaildata(fcustid,fdate,fbilltype,fbillno,fpreamount,freceivableamount,freceiveamount) values(1,'2020-06-30','委托结算','xsd20200700009',0,373.46,0) insert into detaildata(fcustid,fdate,fbilltype,fbillno,fpreamount,freceivableamount,freceiveamount) values(1,'2020-06-30','委托结算退货','xsd20200700010',0,-427.05,0) insert into detaildata(fcustid,fdate,fbilltype,fbillno,fpreamount,freceivableamount,freceiveamount) values(1,'2020-07-30','销售商品返利','xsfl20200700005',0,-17.9,0) insert into detaildata(fcustid,fdate,fbilltype,fbillno,fpreamount,freceivableamount,freceiveamount) values(2,'2020-06-25','预收退款','skd20200700002',-755,0,0) insert into detaildata(fcustid,fdate,fbilltype,fbillno,fpreamount,freceivableamount,freceiveamount) values(2,'2020-06-20','销售发货','xsd20200700006',0,6169.50,6169.50) insert into detaildata(fcustid,fdate,fbilltype,fbillno,fpreamount,freceivableamount,freceiveamount) values(2,'2020-07-30','销售总额返利','xsfl20200700002',0,-493.56,-421.85) insert into detaildata(fcustid,fdate,fbilltype,fbillno,fpreamount,freceivableamount,freceiveamount) values(2,'2020-07-31','其他应收','qtys20200900001',0,6000.00,0) insert into detaildata(fcustid,fdate,fbilltype,fbillno,fpreamount,freceivableamount,freceiveamount) values(2,'2020-06-20','预收冲应收','hxd20200700006',-7245.00,0,7245.00) insert into detaildata(fcustid,fdate,fbilltype,fbillno,fpreamount,freceivableamount,freceiveamount) values(3,'2020-06-30','销售收款','skd20200700003',0,0,2386.96) insert into detaildata(fcustid,fdate,fbilltype,fbillno,fpreamount,freceivableamount,freceiveamount) values(3,'2020-06-30','应收转应收','hxd20200700007',0,2386.75,0) insert into detaildata(fcustid,fdate,fbilltype,fbillno,fpreamount,freceivableamount,freceiveamount) values(3,'2020-07-08','销售退货','xsd20200700014',0,-46.80,0) go
2、以往的游标写法
set nocount on --建立临时表处理获取数据 create table #data( fid int not null primary key identity(1,1), fclasstypeid int not null, fcustid int not null, fnumber nvarchar(255), fname nvarchar(255), fdate datetime null, fbilltype nvarchar(64) null, fbillno nvarchar(64) null, fpreamount decimal(28,10) not null default(0), --预收金额 freceivableamount decimal(28,10) not null default(0), --应收金额 freceiveamount decimal(28,10) not null default(0), --实收金额 fbalanceamount decimal(28,10) not null default(0) --期末余额 ) declare @id int declare @custid int declare @preamount decimal(28,10) declare @receivableamount decimal(28,10) declare @receiveamount decimal(28,10) declare @oldcustid int declare @count int declare @lastamount decimal(28,10) declare @sumpreamount decimal(28,10) declare @sumreceivableamount decimal(28,10) declare @sumreceiveamount decimal(28,10) declare @sumbalanceamount decimal(28,10) --使用游标 declare data_cursor cursor for select fid,fcustid,fpreamount,freceivableamount,freceiveamount from detaildata order by fcustid,fdate,fid open data_cursor fetch next from data_cursor into @id,@custid,@preamount,@receivableamount,@receiveamount set @oldcustid = @custid set @count = 0 set @lastamount = 0 set @sumpreamount = 0 set @sumreceivableamount = 0 set @sumreceiveamount = 0 set @sumbalanceamount = 0 while @@fetch_status = 0 begin if @count > 0 begin if @oldcustid <> @custid begin --表示客户已经变了,要插入小计 set @count = 0 insert into #data(fclasstypeid,fbilltype,fcustid,fnumber,fname,fpreamount,freceivableamount,freceiveamount,fbalanceamount) select -9999,fname + '小计',fitemid,fnumber,fname,@sumpreamount,@sumreceivableamount,@sumreceiveamount,@lastamount from organization where fitemid = @oldcustid select @sumpreamount=0,@sumreceivableamount=0,@sumreceiveamount=0,@sumbalanceamount=0,@lastamount=0 end end if @count = 0 begin set @oldcustid=@custid --插入一行空行 insert into #data(fclasstypeid,fbilltype,fcustid,fnumber,fname) select -1000,fname,fitemid,fnumber,fname from organization where fitemid = @custid --获取期初的期末余额 select @lastamount=isnull(freceivableamount,0) - isnull(fpreamount,0) - isnull(freceiveamount,0),@preamount=isnull(fpreamount,0),@receivableamount=isnull(freceivableamount,0),@receiveamount=isnull(freceiveamount,0) from initialdata where fcustid = @custid insert into #data(fclasstypeid,fbilltype,fcustid,fnumber,fname,fbalanceamount) values(-1000,'期初余额',@custid,'','',@lastamount) select @count = 1 select @sumbalanceamount = @lastamount end --插入单据明细 insert into #data(fclasstypeid,fcustid,fnumber,fname,fdate,fbilltype,fbillno,fpreamount,freceivableamount,freceiveamount,fbalanceamount) select 0,d.fcustid,o.fnumber,o.fname,fdate,fbilltype,fbillno,fpreamount,freceivableamount,freceiveamount,@lastamount + freceivableamount - fpreamount - freceiveamount from detaildata d inner join organization o on d.fcustid = o.fitemid where d.fcustid = @custid and fid = @id select @lastamount = @lastamount + freceivableamount - fpreamount - freceiveamount, @sumpreamount=@sumpreamount + fpreamount,@sumreceivableamount=@sumreceivableamount + freceivableamount, @sumreceiveamount=@sumreceiveamount + freceiveamount from detaildata where fcustid = @custid and fid = @id fetch next from data_cursor into @id,@custid,@preamount,@receivableamount,@receiveamount end if @count > 0 begin insert into #data(fclasstypeid,fbilltype,fcustid,fnumber,fname,fpreamount,freceivableamount,freceiveamount,fbalanceamount) select -9999,fname + '小计',fitemid,fnumber,fname,@sumpreamount,@sumreceivableamount,@sumreceiveamount,@lastamount from organization where fitemid = @oldcustid select @sumpreamount=0,@sumreceivableamount=0,@sumreceiveamount=0,@sumbalanceamount=0,@lastamount=0 end close data_cursor deallocate data_cursor select * from #data order by fcustid,fid drop table #data
代码说明:创建了一个临时表,使用游标遍历我们的detaildata数据表,为了呈现我们最终需要的数据样式,插入客户空行、期初余额、单据信息、客户小计等,逐行计算期末余额值的情况,最终效果如下:
3、使用sum() over()的写法
set nocount on --建立临时表处理获取数据 create table #data( fid int not null primary key identity(1,1), fclasstypeid int not null, fcustid int not null, fnumber nvarchar(255), fname nvarchar(255), fdate datetime null, fbilltype nvarchar(64) null, fbillno nvarchar(64) null, fpreamount decimal(28,10) not null default(0), --预收金额 freceivableamount decimal(28,10) not null default(0), --应收金额 freceiveamount decimal(28,10) not null default(0), --实收金额 fbalanceamount decimal(28,10) not null default(0) --期末余额 ) --插入空行 insert into #data(fclasstypeid,fbilltype,fcustid,fnumber,fname) select -1000,fname,fitemid,fnumber,fname from organization o inner join (select fcustid from detaildata group by fcustid) d on d.fcustid = o.fitemid --插入期初余额 insert into #data(fclasstypeid,fbilltype,fcustid,fnumber,fname,fbalanceamount) select -1000,'期初余额',fitemid,'','',i.freceivableamount - i.fpreamount -i.freceiveamount from organization o inner join initialdata i on o.fitemid = i.fcustid inner join (select fcustid from detaildata group by fcustid) d on d.fcustid = o.fitemid --插入单据明细(关键代码sum() over() ) insert into #data(fclasstypeid,fcustid,fnumber,fname,fdate,fbilltype,fbillno,fpreamount,freceivableamount,freceiveamount,fbalanceamount) select 0,d.fcustid,o.fnumber,o.fname,d.fdate,d.fbilltype,d.fbillno,d.fpreamount,d.freceivableamount,d.freceiveamount, sum(d.freceivableamount - d.fpreamount - d.freceiveamount) over(partition by d.fcustid order by d.fcustid,d.fdate,d.fid) + i.freceivableamount - i.fpreamount - i.freceiveamount from detaildata d with(nolock) inner join organization o with(nolock) on o.fitemid = d.fcustid inner join initialdata i with(nolock) on o.fitemid = i.fcustid order by d.fcustid,d.fdate,d.fid --插入小计 insert into #data(fclasstypeid,fbilltype,fcustid,fnumber,fname,fpreamount,freceivableamount,freceiveamount,fbalanceamount) select -9999,fname + '小计',d.fcustid,fnumber,fname,sum(fpreamount),sum(freceivableamount),sum(freceiveamount),0 from dbo.detaildata d inner join dbo.organization o on d.fcustid = o.fitemid group by d.fcustid,o.fname,o.fnumber --更新小计的期末余额 update d set d.fbalanceamount = d.freceivableamount - d.fpreamount - d.freceiveamount + i.freceivableamount - i.fpreamount - i.freceiveamount from #data d inner join initialdata i on d.fcustid = i.fcustid where d.fclasstypeid = -9999 select * from #data order by fcustid,fid drop table #data
代码说明:相比第二种,去除了游标的写法,通过了
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() 计算客户的订单号
select distinct fcustid,count(fbillno) over(partition by fcustid) fbillnum from detaildata
总结:
1、游标的使用场景可以很广,但是在数据量大的时候,就会显得很慢,一行一行遍历的速度还是挺久的
2、使用开窗函数来实现一些功能,还是很方便能实现效果,并且它的速度也是很快,值得推荐。
到此这篇关于sql server 开窗函数 over()代替游标的使用的文章就介绍到这了,更多相关sql server 开窗函数 over()内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
上一篇: window下创建redis出现问题小结
下一篇: 如何将PDF转换成Word文档的方法总结