1

我有一张如下表

 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

如果输入为 50%

输出应该是

 Board Component Cost

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

等等

如何在sql server中编写查询

4

5 回答 5

1

首先,由于我们需要对以前的值进行分组和求和,因此您需要一个身份来实现这一点。我希望你能想出一个。我创建了这张表来模拟你的情况:

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

结果:

1   c1  5
2   b1  15
3   b2  30
4   b3  50

如果您不希望这样,您可以根据上述选择的 id 在主表上运行选择:

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
)

结果:

1   c1  5
2   b1  10
3   b2  15
4   b3  20
于 2012-05-22T09:14:35.463 回答
1
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)
于 2012-05-22T11:21:07.930 回答
0

试试这个

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
于 2012-05-22T09:22:01.580 回答
0

试试这个:

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;

应该管用

于 2012-05-22T10:12:25.920 回答
0
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
于 2012-05-22T12:45:51.250 回答