Permutation / combination in Sql Server

SQL Server 2005

select top 100 trans_no
, mtrs
, rollno
from pack_local_det_fox
where trans_no = 'A-11790'

and the result is

trans_no      mtrs      rollno
A-11790      162      274542
A-11790      105      274695
A-11790      140      274697
A-11790      140      274698
A-11790      90      274699
A-11790      59      274702
A-11790      150      274555
A-11790      144      454777
A-11790      116      149317
A-11790      140      149318
A-11790      140      274691
A-11790      150      274690

User will provide a mtrs say 746 then our goal is to choose those rows whoose sum of mtrs is
equal to or nearer to 746, taking highest mtrs first.

The resultant rows will be
trans_no      mtrs      rollno
A-11790      162      274542
A-11790      150      274690
A-11790      150      274555
A-11790      144      454777
A-11790      140      274698

total mtrs is exactly 746


if user provide say 807
then the resultant rows should be

trans_no      mtrs      rollno
A-11790      162      274542
A-11790      150      274690
A-11790      150      274555
A-11790      144      454777
A-11790      140      274698
A-11790        59      274702

The sum of mtrs is 805 ( nearer to 807).

Is it possible in sql server?
MehramAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

BardobraveCommented:
Well... I supose you always can do a stored procedure to code something like that... If what you're asking is if it's possible to use a T-SQL statement to obtain such a behaviour I don't think such a thing could be possible.
0
MehramAuthor Commented:
Hi Bardobrave

I know how to make store procedure, but this is very complicated.
0
speight040Commented:
select top 100
trans_no
, sum(mtrs)
, rollno
from pack_local_det_fox
where trans_no = 'A-11790'
group by trans_no, roll_no
having sum(mtrs) > 746
order by sum(mtrs) desc
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

MehramAuthor Commented:
Hi speight040:

Your code

select top 100
trans_no
, sum(mtrs)
, rollno
from pack_local_det_fox
where trans_no = 'A-11790'
group by trans_no, rollno
having sum(mtrs) > 746
order by sum(mtrs) desc

is producing empty result set
0
MehramAuthor Commented:
Hi bardobrave, speight040

I seem to have found a way, (Pl,see code snippet)
but I am not sure, if it will work in all case.
Sure, it is not taking all combination of mtrs into condieration , rather it is doing one way that is
sort the rows by higest mtrs descending and allocate mts 746.



declare @pack_local_det_fox table ( trans_no varchar(8), mtrs smallint, rollno bigint)
insert @pack_local_det_fox 
select 'a-11790', 162, 274542 union all
select 'a-11790', 105, 274695 union all
select 'a-11790', 140, 274697 union all
select 'a-11790', 140, 274698 union all
select 'a-11790', 90, 274699  union all
select 'a-11790', 59, 274702  union all
select 'a-11790', 150, 274555 union all 
select 'a-11790', 144, 454777 union all
select 'a-11790', 116, 149317 union all
select 'a-11790', 140, 149318 union all
select 'a-11790', 140, 274691 union all
select 'a-11790', 150, 274690

;with cte as
( select
  a.trans_no
, a.rollno
, a.mtrs
, rollnosorted = right('00000'+convert(varchar,a.mtrs),5) +'-'+ right('00000000'+convert(varchar,a.rollno),8) /* to make rollno highest according to mtrs*/
from  @pack_local_det_fox  a 
),
cte2 as
( select
  a.trans_no
, a.rollno
, a.mtrs
, allocation = (case when sum(b.mtrs) <= 746 then a.mtrs else
               (case when 746 + a.mtrs >= sum(b.mtrs) 
                then (746 + a.mtrs) - sum(b.mtrs) else 0 end) end) 
, rollnosorted = right('00000'+convert(varchar,a.mtrs),5) +'-'+ right('00000000'+convert(varchar,a.rollno),8)
from  cte a 
join  cte b on ( a.trans_no = b.trans_no and a.rollnosorted <=b.rollnosorted)
group by a.trans_no
, a.rollno
, a.mtrs
)
select *
from cte2
where allocation > 0

Open in new window

0
BardobraveCommented:
I'm sorry, but I'm still thinking that there's no possible solution to this by a T-SQL statement, and if it's exists I'm sure that will not be as easy.

You need a statement that groups and sum values, and you need it also to compare it to a determined value, BUT, you also need it to combine rows within the recordset in all possible grouping/sum orders to obtain a Sum value equal to your determined one OR the nearest one available... way long from T-SQL capabilities, at least from my knowledge, maybe any gurú can make some magic with that, but probably you would wished make the procedure when you see it ;)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
MehramAuthor Commented:
I agee
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.