• Status: Solved
• Priority: Medium
• Security: Public
• Views: 917

# 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?
0
Mehram
• 4
• 2
1 Solution

Commented:
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

Author Commented:
Hi Bardobrave

I know how to make store procedure, but this is very complicated.
0

Commented:
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

Author Commented:
Hi speight040:

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

Author 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
``````
0

Commented:
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

Author Commented:
I agee
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.