Here is one way:----------------------------------------------create the data tableset nocount onif object_id('junk') > 0 drop table junkcreate table junk(rowid int identity(1,1) primary key clustered, n int)insert junk (n)select 1 unionselect 2 unionselect 3 unionselect 4----------------------------------------------create a function to get the product of all preceeding rowsif object_id('dbo.udfButt') > 0 drop function dbo.udfButtgocreate function dbo.udfButt (@n int)returns bigintasbegin declare @bi bigint select @bi = coalesce(@bi * n, n) from junk where n <= @n order by rowid return @biendgo----------------------------------------------use the function for each rowselect n, x = dbo.udfButt(n) from junk order by rowidoutput:n x ----------- -------------------- 1 12 23 64 24
Be One with the OptimizerTG