Sql查询以获取基于%输入的值

Sql查询以获取基于%输入的值,sql,sql-server-2008,sql-server-2005,Sql,Sql Server 2008,Sql Server 2005,我有一张像下面这样的桌子 Board Component Cost b1 c1 5 b1 c2 10 b2 c3 15 b3 c4 20 b3 c5 25 b4 C6 25 --------------------- 100% 我应该给出输入以从表中获取值,比如 如果输入为20% 输出

我有一张像下面这样的桌子

 Board Component Cost

    b1     c1       5
    b1     c2       10
    b2     c3       15
    b3     c4       20
    b3     c5       25
    b4     C6       25
  ---------------------
                  100%
我应该给出输入以从表中获取值,比如

如果输入为20%

输出应该是

 Board Component Cost

    b1      c1      5
    b1      c2     10
    b2      c3     15
 Board Component Cost

    b1      c1      5
    b1      c2     10
    b2      c3     15
    b3      c4     20
如果输入为50%

输出应该是

 Board Component Cost

    b1      c1      5
    b1      c2     10
    b2      c3     15
 Board Component Cost

    b1      c1      5
    b1      c2     10
    b2      c3     15
    b3      c4     20
等等


如何首先在sql server中编写查询,因为我们需要对以前的值进行分组和求和,所以您需要一个标识来实现这一点。 我希望你能想出一个。 我创建此表是为了模拟您的情况:

create table mySUM(
id int identity(1,1),
id2 varchar(50),
cost int)

insert into mySUM values
('c1',  5),   ('b1', 10),    ('b2',15),    ('b3',20),    ('b3',25),    ('b4',25)
我有两个选择给你。 这是主要的一个,但是,由于我们正在分组和求和,它将在最后一列中得出求和的结果:

select a.Id, a.id2, sum(b.cost) as totalCost
from mySUM a cross join mySUM b
where b.Id <= a.Id
group by a.Id,a.id2
having sum(b.cost)<=50
order by  a.Id
如果您不想这样做,可以基于上述select的ID在主表上运行select:

select * from mySUM where id in(
    select a.Id
    from mySUM a cross join mySUM b
    where b.Id <= a.Id
    group by a.Id,a.id2
    having sum(b.cost)<=50
)
试试这个

SELECT  t.* ,
        rt.runningTotal
FROM    Test t
        CROSS APPLY ( SELECT    SUM(cost) AS runningTotal
                      FROM      Test
                      WHERE     Board <= t.Board
                                AND Component <= t.Component
                    ) AS rt
WHERE   runningTotal <= 50
ORDER BY t.Board
试试这个:

with CTE (Board, Component ,Cost, rownum)
as
(
SELECT Board,Component, Cost,
ROW_NUMBER() OVER(order by Cost asc, Board asc, Component asc) rownum
  FROM yourTable t0
  where Cost > 0
)
select
t1.Board,t1.Component, t1.Cost, t1.rownum, sum(t2.Cost)
from 
CTE t1 join CTE t2 on t1.rownum >= t2.rownum
group by t1.Board,t1.Component, t1. Cost, t1.rownum
having sum(t2.Cost) < 50
order by t1.rownum;

应该以50%的比例工作

,为什么最后两个不行?我们应该从上到下选择?20%不应该是30%吗?你想对部分进行求和,直到达到一定的百分比吗?你想按什么排序?这听起来像是子集求和问题是的,首先我需要从低到高排序,如果是50%,它将列出所有。但是不应该,请检查第二个输出表是的,我确实读得太快了。我改变了我的答案,请看一下当你的问题要求20%时,你返回5+10+15=30。这不应该是5+10=15,因为它比20小吗?如果需要按分数排序,请更改内部Where子句。原来的问题不清楚,虽然你想按字母顺序排列前两列。@Somashekhar也许你可以帮助你的助手,说明哪些不正确
DECLARE @T TABLE (Col1 VARCHAR(10), Col2 VARCHAR(10), Col3 INT, SumValue Int)

INSERT INTO @T(Col1, Col2, Col3)
VALUES ('b1','c1',5)

INSERT INTO @T(Col1, Col2, Col3)
VALUES ('b1','c2',10)

INSERT INTO @T(Col1, Col2, Col3)
VALUES ('b2','c3',15)

INSERT INTO @T(Col1, Col2, Col3)
VALUES ('b3','c4',20)

INSERT INTO @T(Col1, Col2, Col3)
VALUES ('b3','c5',25)

INSERT INTO @T(Col1, Col2, Col3)
VALUES ('b4','C6',25) 

DECLARE @SumValue INT

UPDATE @T
SET @SumValue=SumValue = ISNULL(@SumValue,0)+ Col3
FROM @T T

SELECT *
FROM @T
WHERE SumValue <= (
    SELECT TOP 1 SumValue
    FROM @T AS T
    WHERE T.SumValue>=40
    ORDER BY SumValue)
create table #per(
Board varchar(10),
Component varchar(max),
Cost int)

create proc out_per (
@desire_per int)
as begin

declare @sum int, @count int;
declare @board varchar(max), @component varchar(max);
declare @cost int;
declare per_cursor cursor for
select Board,Component,Cost from per
open per_cursor
fetch next from per_cursor into @Board, @component, @cost
while(@@fetch_status=0)
    begin
        insert into #per values (@Board, @component, @cost);
        set @sum=(select sum(cost) from #per)
        if(@sum>=@desire_per)
        begin
            break;
        end
        fetch next from per_cursor into @Board, @component, @cost
    end
    close per_cursor
    deallocate per_cursor
    select * from #per
    truncate table #per
end

exec out_per @desire_per=30