cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Use this SQL with Sybase

Former Member
10,159

I have a good working sql that works in sql server. The logical computation calculates how many lengths will fit a barrel. Maximum capacity per drum is 14300 meters. My question is how I can get this to work with Sybase.

Declare @tblLength table (num int, qty int, unit varchar(20))

insert @tblLength
values
(2,2500,''),
(1,3800,''),
(1,4200,''),
(1,6500,''),
(1,8300,'')

;with cte as
(
select i=1, qty, unit from @tblLength where num >= 1
union all
select i=i+1, t.qty, t.unit from @tblLength t, cte where t.qty=cte.qty and t.unit = cte.unit and t.num >= i+1
),
cte2 as
(
select *, id=row_number() over (order by qty desc) from cte
),
cte3 as
(
select j=1, qty, unit, id, tot=qty, drum=1 from cte2 where id = 1
union all
select j=j+1, cte2.qty, cte2.unit, cte2.id,
tot=case when cte3.tot+cte2.qty > 14300 then cte2.qty else cte3.tot+cte2.qty end,
drum=case when cte3.tot+cte2.qty > 14300 then cte3.drum + 1 else cte3.drum end
from cte2,cte3 where cte2.id = j+1
)
select drum, qty, unit from cte3
View Entire Topic
Former Member
0 Likes

My simplistic attempt to split this into 2 parts with the use of a stored procedure fails. I suspect that may be because of inlining ...

Maybe some other readers have a better approach.

Former Member
0 Likes

It is best to try to do it without SP. But I can not do it, unfortunately. And it is perhaps as you say, it may not be possible without splitting...

Breck_Carter
Participant

Sadly, I'd love to help, but I still don't have a clue about what the query is trying to accomplish... in spite of your valiant attempts to explain it. Perhaps if you explained it as if you were speaking to a kindergarten class...

alt text