Recent PostsRecent Posts Popular TopicsPopular Topics
 Home Search Members Calendar Who's On

 Splitting the rows -- distributing on logic Rate Topic Display Mode Topic Options
Author
 Message
 Posted Tuesday, April 02, 2013 3:02 AM
 SSC Journeyman Group: General Forum Members Last Login: Friday, November 29, 2013 5:41 PM Points: 94, Visits: 706
 Hi Experts , here is sample table and data create table #sample ( Invtid varchar(255) ,[Final SiteId] varchar(255) ,Whseloc varchar(255) ,Days int ,Qty int ,[Aging Stock] int ,Priority int)insert into #sampleselect '11003291001-----NOB30000','0V003','A00A00',269,285,305,1 union allselect '11003291001-----NOB30000','03003','A00A00',287,30,305,2-- Expected output Invtid Siteid Whseloc Days Qty11003291001-----NOB30000 0V003 A00A00 269 28511003291001-----NOB30000 03003 A00A00 269 20 -- 305-285=20 (20 out of 30)11003291001-----NOB30000 03003 A00A00 287 10 -- 10 out of 30here 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 lines11003291001-----NOB30000 0V003 A00A00 269 285 ( First Allocation) 11003291001-----NOB30000 03003 A00A00 269 20 ( 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 record11003291001-----NOB30000 03003 287 10The 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 records11003291001-----NOB30000 0V003 A00A00 269 285 ( First Allocation) 11003291001-----NOB30000 03003 A00A00 269 20 ( Second Allocation ) 11003291001-----NOB30000 03003 A00A00 287 10 ( Third Allocation )please help me ..
Post #1437758
 Posted Tuesday, April 02, 2013 8:07 AM
 SSChampion Group: General Forum Members Last Login: Wednesday, December 04, 2013 2:23 PM Points: 10,854, Visits: 10,012
 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 Moden's splitter.Cross Tabs and Pivots, Part 1 – Converting Rows to Columns Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs Understanding and Using APPLY (Part 1)Understanding and Using APPLY (Part 2)
Post #1437910
 Posted Tuesday, April 02, 2013 8:31 AM
 SSC Journeyman Group: General Forum Members Last Login: Friday, November 29, 2013 5:41 PM Points: 94, Visits: 706
 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 allselect '11003291001-----NOB30000','03003','A00A00',287,30,305,2output: Invtid Siteid Whseloc Days Qty11003291001-----NOB30000 0V003 A00A00 269 28511003291001-----NOB30000 03003 A00A00 269 20 -- 305-285=20 (20 out of 30)11003291001-----NOB30000 03003 A00A00 287 10 -- 10 out of 30output 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 daysoutput record 3 ) in input record 2 , from qty 30 we placed 20 in row2 so final output row can have 10 Qty
Post #1437933
 Posted Tuesday, April 02, 2013 8:47 AM
 SSChampion Group: General Forum Members Last Login: Wednesday, December 04, 2013 2:23 PM Points: 10,854, Visits: 10,012
 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 allselect '11003291001-----NOB30000','03003','A00A00',287,30,305,2output: Invtid Siteid Whseloc Days Qty11003291001-----NOB30000 0V003 A00A00 269 28511003291001-----NOB30000 03003 A00A00 269 20 -- 305-285=20 (20 out of 30)11003291001-----NOB30000 03003 A00A00 287 10 -- 10 out of 30output 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 daysoutput record 3 ) in input record 2 , from qty 30 we placed 20 in row2 so final output row can have 10 QtyNo 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 Moden's splitter.Cross Tabs and Pivots, Part 1 – Converting Rows to Columns Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs Understanding and Using APPLY (Part 1)Understanding and Using APPLY (Part 2)
Post #1437942
 Posted Tuesday, April 02, 2013 9:20 AM
 SSC Journeyman Group: General Forum Members Last Login: Friday, November 29, 2013 5:41 PM Points: 94, Visits: 706
 Input rows:days Qty [Aging Stock] priority 269 285 305 1287 30 300 2output:269 285 305 1 -- same as input row1 269 20 305 2 -- few qty from row-2 to fill row1 qty287 10 300 2 -- remaining qty from row-2 Explantion for output:ouput row 1) same as input rowouput 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
Post #1437961
 Posted Tuesday, April 02, 2013 9:24 AM
 SSChampion Group: General Forum Members Last Login: Wednesday, December 04, 2013 2:23 PM Points: 10,854, Visits: 10,012
 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 Moden's splitter.Cross Tabs and Pivots, Part 1 – Converting Rows to Columns Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs Understanding and Using APPLY (Part 1)Understanding and Using APPLY (Part 2)
Post #1437965
 Posted Tuesday, April 02, 2013 11:28 PM
 SSC Journeyman Group: General Forum Members Last Login: Friday, November 29, 2013 5:41 PM Points: 94, Visits: 706
 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 .
Post #1438171
 Posted Wednesday, April 03, 2013 2:01 AM
 SSCertifiable Group: General Forum Members Last Login: Yesterday @ 7:32 AM Points: 6,274, Visits: 12,089
 What's confusing folks here is that this sample data`insert into #sampleselect '11003291001-----NOB30000','0V003','A00A00',269,285,305,1 union allselect '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. “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail ShawFor 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 ModenExploring Recursive CTEs by Example Dwain Camps
Post #1438197
 Posted Wednesday, April 03, 2013 7:07 AM
 SSC-Enthusiastic Group: General Forum Members Last Login: Yesterday @ 5:58 AM Points: 112, Visits: 506
 The way I'd read the requirement is as follows:-Qty is, well, a Quantity.Aging Stock is a CapacityPriority 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.
Post #1438307
 Posted Wednesday, April 03, 2013 8:45 AM
 SSC Journeyman Group: General Forum Members Last Login: Friday, November 29, 2013 5:41 PM Points: 94, Visits: 706
 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 CapacityPriority 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.
Post #1438396

 Permissions