回复: 用集合的方式求积
create table t
(
num int
)
insert into t select 5
insert into t select 2
insert into t select 3
insert into t select -1
go
;with nt as
(
select id=row_number() over (order by num), num from t
)
, cte as
(
select id, num from nt where id=1
union all
select nt.id, num=nt.num*cte.num
from nt join cte on nt.id=cte.id+1
)
select top 1 num from cte
order by id desc
/*
-30
*/
insert t select -2
;with nt as
(
select id=row_number() over (order by num), num from t
)
, cte as
(
select id, num from nt where id=1
union all
select nt.id, num=nt.num*cte.num
from nt join cte on nt.id=cte.id+1
)
select top 1 num from cte
order by id desc
/*
60
*/
drop table t