Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Splitting the rows -- distributing on logic Expand / Collapse
Author
Message
Posted Tuesday, April 2, 2013 3:02 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Sunday, February 23, 2014 12:27 AM
Points: 94, Visits: 717
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 #sample
select '11003291001-----NOB30000','0V003','A00A00',269,285,305,1 union all
select '11003291001-----NOB30000','03003','A00A00',287,30,305,2

-- Expected output
Invtid Siteid Whseloc Days Qty
11003291001-----NOB30000 0V003 A00A00 269 285
11003291001-----NOB30000 03003 A00A00 269 20 -- 305-285=20 (20 out of 30)
11003291001-----NOB30000 03003 A00A00 287 10 -- 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 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 record
11003291001-----NOB30000 03003 287 10

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 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 2, 2013 8:07 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 8:55 AM
Points: 13,059, Visits: 11,886
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 2, 2013 8:31 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Sunday, February 23, 2014 12:27 AM
Points: 94, Visits: 717
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 Whseloc Days Qty
11003291001-----NOB30000 0V003 A00A00 269 285
11003291001-----NOB30000 03003 A00A00 269 20 -- 305-285=20 (20 out of 30)
11003291001-----NOB30000 03003 A00A00 287 10 -- 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
Post #1437933
Posted Tuesday, April 2, 2013 8:47 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 8:55 AM
Points: 13,059, Visits: 11,886
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 Whseloc Days Qty
11003291001-----NOB30000 0V003 A00A00 269 285
11003291001-----NOB30000 03003 A00A00 269 20 -- 305-285=20 (20 out of 30)
11003291001-----NOB30000 03003 A00A00 287 10 -- 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 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 2, 2013 9:20 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Sunday, February 23, 2014 12:27 AM
Points: 94, Visits: 717
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
Post #1437961
Posted Tuesday, April 2, 2013 9:24 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 8:55 AM
Points: 13,059, Visits: 11,886
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 2, 2013 11:28 PM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Sunday, February 23, 2014 12:27 AM
Points: 94, Visits: 717

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 3, 2013 2:01 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 8:45 AM
Points: 7,107, Visits: 13,461
What's confusing folks here is that this sample data
insert 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.


“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

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
Exploring Recursive CTEs by Example Dwain Camps
Post #1438197
Posted Wednesday, April 3, 2013 7:07 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Yesterday @ 7:57 AM
Points: 121, Visits: 602
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.
Post #1438307
Posted Wednesday, April 3, 2013 8:45 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Sunday, February 23, 2014 12:27 AM
Points: 94, Visits: 717
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.
Post #1438396
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse