Log in  ::  Register  ::  Not logged in

## Splitting the rows -- distributing on logic

 Author Message Nagaram Ten Centuries Group: General Forum Members Points: 1014 Visits: 803 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 .. Sean Lange SSC Guru Group: General Forum Members Points: 147539 Visits: 18565 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.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) Nagaram Ten Centuries Group: General Forum Members Points: 1014 Visits: 803 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 Sean Lange SSC Guru Group: General Forum Members Points: 147539 Visits: 18565 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 Modens 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) Nagaram Ten Centuries Group: General Forum Members Points: 1014 Visits: 803 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 Sean Lange SSC Guru Group: General Forum Members Points: 147539 Visits: 18565 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. :-D _______________________________________________________________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.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) Nagaram Ten Centuries Group: General Forum Members Points: 1014 Visits: 803 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 . ChrisM@Work SSC Guru Group: General Forum Members Points: 96209 Visits: 20661 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 FunkyDexter SSCommitted Group: General Forum Members Points: 1824 Visits: 1076 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. Nagaram Ten Centuries Group: General Forum Members Points: 1014 Visits: 803 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.

## Permissions

 You can't post new topics. You can't post topic replies. You can't post new polls. You can't post replies to polls. You can't edit your own topics. You can't delete your own topics. You can't edit other topics. You can't delete other topics. You can't edit your own posts. You can't edit other posts. You can't delete your own posts. You can't delete other posts. You can't post events. You can't edit your own events. You can't edit other events. You can't delete your own events. You can't delete other events. You can't send private messages. You can't send emails. You can read topics. You can't vote in polls. You can't upload attachments. You can download attachments. You can't post HTML code. You can't edit HTML code. You can't post IFCode. You can't post JavaScript. You can post emoticons. You can't post or upload images.

##### Select a forum
 SQL Server 2017      SQL Server 2017 - Administration      SQL Server 2017 - Development SQL Server 2016      SQL Server 2016 - Administration      SQL Server 2016 - Development and T-SQL SQL Server 2014      Administration - SQL Server 2014      Development - SQL Server 2014 SQL Server 2012      SQL 2012 - General      SQL Server 2012 - T-SQL SQL Server vNext      SQL Server 15 - Administration      SQL Server 15 - Development SQL Server 2008      SQL Server 2008 - General      T-SQL (SS2K8)      June 2007 CTP      Working with Oracle      July CTP      SQL Server Newbies      Security (SS2K8)      SQL Server 2008 High Availability      SQL Server 2008 Administration      Data Corruption (SS2K8 / SS2K8 R2)      SQL Server 2008 Performance Tuning Cloud Computing      SQL Azure - Development      SQL Azure - Administration      Amazon AWS and other cloud vendors      General Cloud Computing Questions      CosmosDB      Azure Data Lake      Azure Machine Learning Reporting Services      Reporting Services      Reporting Services 2005 Administration      Reporting Services 2005 Development      Reporting Services 2008/R2 Administration      Reporting Services 2008 Development      SSRS 2012      SSRS 2014      SSRS 2016 Programming      Connecting      General      SMO/RMO/DMO      XML      Service Broker      Powershell      Testing      TFS/Data Dude/DBPro      SSDT      Continuous Integration, Deployment, and Delivery      R Services and R Language Data Warehousing      Integration Services      Strategies and Ideas      Analysis Services      Data Transformation Services (DTS)      Performance Point      Data Mining      PowerPivot      R language      Machine Learning Database Design      Disaster Recovery      Design Ideas and Questions      Relational Theory      Hardware      Virtualization      Security and Auditing SQLServerCentral.com      Anything that is NOT about SQL!      Contests!      Editorials      SQLServerCentral.com Announcements      SQLServerCentral.com Website Issues      Suggestions      Tag Issues with Content      Podcast Feedback      SQLServerCentral.com Test Forum      Articles Requested SQL Server 2005      Administering      Backups      Business Intelligence      CLR Integration and Programming.      Data Corruption      Development      Working with Oracle      SQL Server 2005 Compact Edition      SQL Server 2005 General Discussion      SQL Server 2005 Security      SQL Server 2005 Strategies      SS2K5 Replication      SQL Server Express      SQL Server 2005 Performance Tuning      SQL Server 2005 Integration Services      T-SQL (SS2K5)      SQL Server Newbies SQL Server 7,2000      Administration      Backups      Data Corruption      General      Globalization      In The Enterprise      Working with Oracle      Security      Strategies      SQL Server Newbies      Service Packs      SQL Server CE      Performance Tuning      Replication      Sarbanes-Oxley      T-SQL      SQL Server Agent SQL Server and other platforms      MySQL      Oracle      PostgreSQL      DB2      SQL Server and Sharepoint Older Versions of SQL (v6.5, v6.0, v4.2)      Older Versions of SQL (v6.5, v6.0, v4.2) Career      Certification      Employers and Employees      Events      Job Postings      Resumes and Job Hunters      Presentations and Speaking      Retired Members Testing Center      SQL Server Security Skills      Question of the Day (QOD) Microsoft Access      Microsoft Access Products and Books      Third Party Products         SQLCentric         Extreme Technologies.         Innovartis         Embarcadero         SQL Sentry         Sonasoft         Golden Gate Software         Idera         Lumigent         Red Gate Software         ApexSQL         Quest Software         CA      Discussions about Books         Discuss Programming Books          Discuss XML Books          Discuss T-SQL Books          Discuss Data Warehousing Books          Discuss DTS Books          Discuss SQL Server 2000 Books         Discuss SQL Server 7.0 Books Notification Services      Administration Article Discussions Future Versions      SQL 12

## Search

 Copyright © 2002-2018 Redgate. All Rights Reserved. Privacy Policy. Terms of Use. Report Abuse.