Select 条记录总列为 600
Select records to sum column to 600
我有一个 table,其中有一列名为 "price",如果我对该列中的所有记录求和,结果为 1000,但我只需要 select获得 600 的结果所必需的,我该怎么做?
我试过用游标来做,但没用
declare cursor1 cursor
for select* from SERVICES
open cursor1
while((select SUM(Price) from SERVICES)=600)
begin
fetch next from cursor1
select SUM(Price) from SERVICES
end
close cursor1
deallocate cursor1
这是一个演示如何使用 window 函数的选项
需要说明的是,结果可能不完全是 600。
例子
Select *
From (
Select *
,RT = sum(Price) over (Order by Price)
From YourTable
) A
Where RT<=600
我有一个 table,其中有一列名为 "price",如果我对该列中的所有记录求和,结果为 1000,但我只需要 select获得 600 的结果所必需的,我该怎么做?
我试过用游标来做,但没用
declare cursor1 cursor
for select* from SERVICES
open cursor1
while((select SUM(Price) from SERVICES)=600)
begin
fetch next from cursor1
select SUM(Price) from SERVICES
end
close cursor1
deallocate cursor1
这是一个演示如何使用 window 函数的选项
需要说明的是,结果可能不完全是 600。
例子
Select *
From (
Select *
,RT = sum(Price) over (Order by Price)
From YourTable
) A
Where RT<=600