April 2, 2013 at 3:02 am
Hi Experts ,
here is sample table and data
create table #sample
(
Invtidvarchar(255)
,[Final SiteId]varchar(255)
,Whselocvarchar(255)
,Daysint
,Qtyint
,[Aging Stock]int
,Priority int
)
insert into #sample
select '11003291001-----NOB30000','0V003','A00A00',269,285,305,1 union all
select '11003291001-----NOB30000','03003','A00A00',287,30,305,2
-- Expected output
InvtidSiteidWhselocDaysQty
11003291001-----NOB30000 0V003A00A00269285
11003291001-----NOB30000 03003 A00A0026920 -- 305-285=20 (20 out of 30)
11003291001-----NOB30000 03003 A00A0028710 -- 10 out of 30
here is the logic:
row 1 priority column 1 first distribution i allocated 285 units.
But the bucket at the [Aging Stock] had originally 305 units
Then the balance between the 305 - 285 = 20 units.
That means that i still have 20 units in stock with 269 days.
So i will allocate only the 20 units to the priority 2[Aging Stock] (20 from 30)
Now in the final table i have 2 lines
11003291001-----NOB30000 0V003A00A00269285 ( First Allocation)
11003291001-----NOB30000 03003A00A0026920 ( Second Allocation -- from row 2)
But there are still 10 units left from the priority 2
So now if i look again to the priority 1 [Aging Stock], i can't work with the 269 days,
since now there is 0 balance there, i use the 10 left for the second allocation.
So i move to the next record
11003291001-----NOB30000 03003 28710
The next record has exactly the same 10 units i need,
and i insert those 10 units with 287 days to the final table.
Now the final output will have 3 records
11003291001-----NOB30000 0V003A00A00269285 ( First Allocation)
11003291001-----NOB30000 03003A00A0026920 ( Second Allocation )
11003291001-----NOB30000 03003A00A0028710 ( Third Allocation )
please help me ..
April 2, 2013 at 8:07 am
Great job posting ddl and sample data. After that though whatever you are trying to do is totally unclear. Can you try explaining again what it is you are looking for?
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 β Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
April 2, 2013 at 8:31 am
I edited the main post .
Sean Lange let me know till if it not clear .. My english is not so good
Here is the logic:
select '11003291001-----NOB30000','0V003','A00A00',269,285,305,1 union all
select '11003291001-----NOB30000','03003','A00A00',287,30,305,2
output:
Invtid Siteid WhselocDaysQty
11003291001-----NOB30000 0V003A00A00269285
11003291001-----NOB30000 03003 A00A0026920 -- 305-285=20 (20 out of 30)
11003291001-----NOB30000 03003 A00A0028710 -- 10 out of 30
output record 1) priority row 1
output record 2) as per priority row 1 we can store more 20 Qty (from row-1, 305-285=20) , so in input row-2 , we can place 20 out of 30 for row1 days
output record 3 ) in input record 2 , from qty 30 we placed 20 in row2 so final output row can have 10 Qty
April 2, 2013 at 8:47 am
Nagaram (4/2/2013)
I edited the main post .Sean Lange let me know till if it not clear .. My english is not so good
Here is the logic:
select '11003291001-----NOB30000','0V003','A00A00',269,285,305,1 union all
select '11003291001-----NOB30000','03003','A00A00',287,30,305,2
output:
Invtid Siteid WhselocDaysQty
11003291001-----NOB30000 0V003A00A00269285
11003291001-----NOB30000 03003 A00A0026920 -- 305-285=20 (20 out of 30)
11003291001-----NOB30000 03003 A00A0028710 -- 10 out of 30
output record 1) priority row 1
output record 2) as per priority row 1 we can store more 20 Qty (from row-1, 305-285=20) , so in input row-2 , we can place 20 out of 30 for row1 days
output record 3 ) in input record 2 , from qty 30 we placed 20 in row2 so final output row can have 10 Qty
No worries about the English. You seem to do fine. Besides t-sql is the language of choice around here. π
I am a little confused by where the logic comes from. I don't quite get what you are trying to do. I don't get why you have 3 rows now instead of 2. I think you want a select statement to split these by some logic but I can't follow it. Maybe you are trying to allocate 305 units ordered by priority? That is still only 2 rows no matter how I slice it. 285 for the first and the remaining 20 for the second. Help me understand.
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 β Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
April 2, 2013 at 9:20 am
Input rows:
days Qty [Aging Stock] priority
269 285 305 1
287 30 300 2
output:
269 285 305 1 -- same as input row1
269 20 305 2 -- few qty from row-2 to fill row1 qty
287 10 300 2 -- remaining qty from row-2
Explantion for output:
ouput row 1) same as input row
ouput row 2) in input row 1 in 269 days still we can store more 20 qty (305-285) , So from input row 2 values we can store 20 Qtys from 30 Qty
ouput row 3) remaining 10qtys from input row 2
April 2, 2013 at 9:24 am
I just can't seem to figure out what it is you want. I have called for reinforcements. We will get you some help on this. π
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 β Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
April 2, 2013 at 11:28 pm
I am a little confused by where the logic comes from. I don't quite get what you are trying to do. I don't get why you have 3 rows now instead of 2. I think you want a select statement to split these by some logic but I can't follow it. Maybe you are trying to allocate 305 units ordered by priority? That is still only 2 rows no matter how I slice it. 285 for the first and the remaining 20 for the second. Help me understand.
this is correct
That is still only 2 rows no matter how I slice it. 285 for the first and the remaining 20 for the second
remaining 10 from second input records is the third record of output .
April 3, 2013 at 2:01 am
What's confusing folks here is that this sample datainsert into #sample
select '11003291001-----NOB30000','0V003','A00A00',269,285,305,1 union all
select '11003291001-----NOB30000','03003','A00A00',287,30,305,2
is both an order and a description of the available stock. Split them out and the problem should become a lot easier to understand and code up.
Also [aging stock] and [days]: you're indicating you have 305 units at 269 days and 305 at 287 days, but you've actually got 305 units, correct? This is confusing and requires clarification.
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
April 3, 2013 at 7:07 am
The way I'd read the requirement is as follows:-
Qty is, well, a Quantity.
Aging Stock is a Capacity
Priority is the order in which the quantities should be used to fill a capacity.
The query required is an attempt to front load the qty into the capacity.
So that total quantity at play here is actually 315 (285 + 30) and the capacity for a given "day" is 305.
The first record to be returned is 269, 285, 305, 1 because day 269 has a capacity of 305 and a qty of 285.
The second record to be returned is 269, 20, 305, 2 because day 269 had spare capacity of 20 (305 - 285) which can be dragged forward from day 287.
The third record to be returned is 287,10,305,2 because day 287 hs a capacity of 305 and a quantity of 10 (after 20 qty was dragged forward to day 269.
Presumably that pattern should continue through the recordset in priority order.
I've got to be honest, I've no idea how you'd achieve that in a sql statment and I think you're into the terrirtory of some sort of loop but there are greater minds than I here who might be able to suggest a good way of achieving it.
April 3, 2013 at 8:45 am
yes FunkyDexter , you're correct .. I am looking for the solution
FunkyDexter (4/3/2013)
The way I'd read the requirement is as follows:-Qty is, well, a Quantity.
Aging Stock is a Capacity
Priority is the order in which the quantities should be used to fill a capacity.
The query required is an attempt to front load the qty into the capacity.
So that total quantity at play here is actually 315 (285 + 30) and the capacity for a given "day" is 305.
The first record to be returned is 269, 285, 305, 1 because day 269 has a capacity of 305 and a qty of 285.
The second record to be returned is 269, 20, 305, 2 because day 269 had spare capacity of 20 (305 - 285) which can be dragged forward from day 287.
The third record to be returned is 287,10,305,2 because day 287 hs a capacity of 305 and a quantity of 10 (after 20 qty was dragged forward to day 269.
Presumably that pattern should continue through the recordset in priority order.
I've got to be honest, I've no idea how you'd achieve that in a sql statment and I think you're into the terrirtory of some sort of loop but there are greater minds than I here who might be able to suggest a good way of achieving it.
Viewing 10 posts - 1 through 9 (of 9 total)
You must be logged in to reply to this topic. Login to reply