Sql Query根据输入的百分比获取值

时间:2012-05-22 09:06:01

标签: 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

如果输入为50%

输出应为

 Board Component Cost

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

等等

如何在sql server中编写查询

5 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分: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)

答案 2 :(得分: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

答案 3 :(得分: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;

应该有效

答案 4 :(得分: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