| Author |
Topic |
|
Bex
Aged Yak Warrior
580 Posts |
Posted - 2006-11-14 : 05:52:46
|
I have the following data:number SetQuantity Desc LineQuantity1 2 NULL 12 2 NULL 11 2 Table 12 2 Table 11 2 Chair 42 2 Chair 43 2 Chair 44 2 Chair 45 2 Chair 46 2 Chair 47 2 Chair 48 2 Chair 4 I want to add a column to assign the items to a set. In this scenario, a set equals 1 table, and 4 chairs, like so:1 2 NULL 1 Set 11 2 Table 1 Set 11 2 Chair 4 Set 12 2 Chair 4 Set 13 2 Chair 4 Set 14 2 Chair 4 Set 1However, if a customer orders more than 1 set (SetQuantity), then I need to relate the items to 2 sets, like so:1 2 NULL 1 Set 12 2 NULL 1 Set 21 2 Table 1 Set 12 2 Table 1 Set 21 2 Chair 4 Set 12 2 Chair 4 Set 13 2 Chair 4 Set 14 2 Chair 4 Set 15 2 Chair 4 Set 26 2 Chair 4 Set 27 2 Chair 4 Set 28 2 Chair 4 Set 2The setquantity is dynamic, so I need an algorithm that can deduce which set the item belongs to depending on the SetQuantity, LineQuantity and number field. For example, dynamically build a range table??????Anyway, if anyone can deduce a mathematical function that can derive this on the fly, I would be eternally grateful!!Hearty head pats |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-11-14 : 06:09:35
|
So this is not ok then?-- prepare test datadeclare @test table (number int, SetQuantity int, [Desc] varchar(50), LineQuantity int)insert @testselect 1, 2, NULL, 1 union allselect 2, 2, NULL, 1 union allselect 1, 2, 'Table', 1 union allselect 2, 2, 'Table', 1 union allselect 1, 2, 'Chair', 4 union allselect 2, 2, 'Chair', 4 union allselect 3, 2, 'Chair', 4 union allselect 4, 2, 'Chair', 4 union allselect 5, 2, 'Chair', 4 union allselect 6, 2, 'Chair', 4 union allselect 7, 2, 'Chair', 4 union allselect 8, 2, 'Chair', 4-- do the workselect number, setquantity, [desc], linequantity, 1 + (number - 1) / linequantity as newcolumnfrom @test Peter LarssonHelsingborg, Sweden |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-11-14 : 06:11:05
|
| [code]-- prepare test datadeclare @test table (number int, SetQuantity int, [Desc] varchar(50), LineQuantity int)insert @testselect 1, 2, NULL, 1 union allselect 1, 2, 'Table', 1 union allselect 1, 2, 'Chair', 4 union allselect 2, 2, 'Chair', 4 union allselect 3, 2, 'Chair', 4 union allselect 4, 2, 'Chair', 4-- do the workselect number, setquantity, [desc], linequantity, 1 + (number - 1) / linequantity as newcolumnfrom @test[/code]Peter LarssonHelsingborg, Sweden |
 |
|
|
Bex
Aged Yak Warrior
580 Posts |
Posted - 2006-11-14 : 06:31:02
|
| Peter, once again you have come through with a great result. Thankyou so much!!! You're a GENIUS!!!Hearty head pats |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-11-14 : 07:17:01
|
| I'm happy it worked out for you, even if the answer was not that advanced.Peter LarssonHelsingborg, Sweden |
 |
|
|
|
|
|