Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
Chet
Starting Member
5 Posts |
Posted - 2008-06-03 : 13:17:04
|
| Hello,I am a little new at T-SQL. and I could use a suggestion on the best way to accomplish this task. I have an application where I must loop through a table in Microsoft SQL Server 2005, find the records that match and break them up into groups where the value of the Thickness field is in groups of ( <=8). Lets say I have 100 records and the table contains sacks marked A, B, C, and D. Within Sack A is 6 records. Each of the 6 records has a thickness column with a value between 0.5 to 1.0 (my example shows 2) I have a column called bundle that I must update to show a 1 for the first group of (<=8) inches thick a 2 for the second group (<=8) inches thick and so on. Then repeat the process for sacks B, C and D. The table itself has the followingOrder Number |Zipcode |Sack(A, B, C,or D)| Thickness |Bundle|1 |19809 |A | 2 |NULL |2 |19809 |A | 2 |NULL3 |19809 |A | 2 |NULL4 |19809 |A | 2 |NULL5 |19809 |A | 2 |NULL6 |19809 |A | 2 |NULL |7 |19721 |B | 4 |NULL |8 |19721 |B | 3 |NULL |9 |19721 |B | 2 |NULL |10 |19721 |B | 5 |NULL |11 |19721 |B | 2 |NULL |12 |19721 |B | 2 |NULL |What I need to do is sum the thickness until it gets to less than 8 and update bundle with a 1. The result would be 4 records and the table would then look like this:Order Number |Zipcode | Sack(A, B, C,or D)| Thickness |Bundle|1 |19809 |A | 2 |1 |2 |19809 |A | 2 |13 |19809 |A | 2 |14 |19809 |A | 2 |15 |19809 |A | 2 |NULL6 |19809 |A | 2 |NULL |7 |19721 |B | 4 |NULL |8 |19721 |B | 3 |NULL |9 |19721 |B | 2 |NULL |10 |19721 |B | 5 |NULL |11 |19721 |B | 2 |NULL |12 |19721 |B | 2 |NULL |Notice there are two records that are not bundles for sack A. I need to place them in a separate bundle called 2. The result will be as followsOrder Number |Zipcode | Sack(A, B, C,or D)| Thickness |Bundle|1 |19809 |A | 2 |1 |2 |19809 |A | 2 |13 |19809 |A | 2 |14 |19809 |A | 2 |15 |19809 |A | 2 |26 |19809 |A | 2 |2 |7 |19721 |B | 4 |NULL|8 |19721 |B | 3 |NULL 9 |19721 |B | 2 |NULL10 |19721 |B | 5 |NULL |11 |19721 |B | 2 |NULL |12 |19721 |B | 2 |NULL |Now Sack A is complete so I must move on to Sack B. The first two records total to 7 and adding another record would move the thickness past 8 so I must update the bundle for only the first two records with a 1. The next two total to seven also and the following 2 total to four. We must set those bundles to 1, 2, 3 as follows1 |19809 |A | 2 |1 |2 |19809 |A | 2 |13 |19809 |A | 2 |14 |19809 |A | 2 |15 |19809 |A | 2 |26 |19809 |A | 2 |2 |7 |19721 |B | 4 |1 |8 |19721 |B | 3 |1 |9 |19721 |B | 2 |2 |10 |19721 |B | 5 |2 |11 |19721 |B | 2 |3 |12 |19721 |B | 2 |3|These scenarios just keep repeating until the last recordThank you for any help you can give me |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2008-06-03 : 13:20:49
|
| Please post a more detailed data example so that your problem is clear.Tara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/Database maintenance routines:http://weblogs.sqlteam.com/tarad/archive/2004/07/02/1705.aspx |
 |
|
|
Chet
Starting Member
5 Posts |
Posted - 2008-06-03 : 14:15:37
|
| OK I’ll give it a try. The table itself has the followingOrder Number |Zipcode |Sack(A, B, C,or D)| Thickness |Bundle|1 |19809 |A | 2 |NULL |2 |19809 |A | 2 |NULL3 |19809 |A | 2 |NULL4 |19809 |A | 2 |NULL5 |19809 |A | 2 |NULL6 |19809 |A | 2 |NULL |7 |19721 |B | 4 |NULL |8 |19721 |B | 3 |NULL |9 |19721 |B | 2 |NULL |10 |19721 |B | 5 |NULL |11 |19721 |B | 2 |NULL |12 |19721 |B | 2 |NULL |What I need to do is sum the thickness until it gets to less than 8 and update bundle with a 1. The result would be 4 records and the table would then look like this:Order Number |Zipcode | Sack(A, B, C,or D)| Thickness |Bundle|1 |19809 |A | 2 |1 |2 |19809 |A | 2 |13 |19809 |A | 2 |14 |19809 |A | 2 |15 |19809 |A | 2 |NULL6 |19809 |A | 2 |NULL |7 |19721 |B | 4 |NULL |8 |19721 |B | 3 |NULL |9 |19721 |B | 2 |NULL |10 |19721 |B | 5 |NULL |11 |19721 |B | 2 |NULL |12 |19721 |B | 2 |NULL |Notice there are two records that are not bundles for sack A. I need to place them in a separate bundle called 2. The result will be as followsOrder Number |Zipcode | Sack(A, B, C,or D)| Thickness |Bundle|1 |19809 |A | 2 |1 |2 |19809 |A | 2 |13 |19809 |A | 2 |14 |19809 |A | 2 |15 |19809 |A | 2 |26 |19809 |A | 2 |2 |7 |19721 |B | 4 |NULL|8 |19721 |B | 3 |NULL 9 |19721 |B | 2 |NULL10 |19721 |B | 5 |NULL |11 |19721 |B | 2 |NULL |12 |19721 |B | 2 |NULL |Now Sack A is complete so I must move on to Sack B. The first two records total to 7 and adding another record would move the thickness past 8 so I must update the bundle for only the first two records with a 1. The next two total to seven also and the following 2 total to four. We must set those bundles to 1, 2, 3 as follows1 |19809 |A | 2 |1 |2 |19809 |A | 2 |13 |19809 |A | 2 |14 |19809 |A | 2 |15 |19809 |A | 2 |26 |19809 |A | 2 |2 |7 |19721 |B | 4 |1 |8 |19721 |B | 3 |1 |9 |19721 |B | 2 |2 |10 |19721 |B | 5 |2 |11 |19721 |B | 2 |3 |12 |19721 |B | 2 |3|These scenarios just keep repeating until the last recordThank you |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-06-04 : 14:15:32
|
May be this:-UPDATE tSET t.Bundle=CASE WHEN (b.BundleCount%8)>0 THEN (b.BundleCount/8)+ 1 ELSE (b.BundleCount/8) ENDFROM YourTable tCROSS APPLY (SELECT SUM(Thickness) AS BundleCount FROM Table WHERE Sack=t.Sack AND OrderNumber<=t.OrderNumber)b |
 |
|
|
Chet
Starting Member
5 Posts |
Posted - 2008-06-04 : 15:23:25
|
| Thank you for the reply. I tried the code and got the followingMSG 403, Level 16, State 1, Line 1The data types float and int are incompatible in the modulo operatorwhatever that is |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-06-05 : 00:27:31
|
quote: Originally posted by Chet Thank you for the reply. I tried the code and got the followingMSG 403, Level 16, State 1, Line 1The data types float and int are incompatible in the modulo operatorwhatever that is
i think Thickness is of type float. Try casting it to int before doing this if you're sure that they dont contain decimal values. |
 |
|
|
Chet
Starting Member
5 Posts |
Posted - 2008-06-05 : 08:26:45
|
| I see...That field will contain decimals as I must calculate the actual thickness of each piece. Thank you |
 |
|
|
Chet
Starting Member
5 Posts |
Posted - 2008-06-05 : 13:06:19
|
| Hey That worked like a charm..Thank you very much for your help. I would have never figured that one out on my own. The following was the solutionUPDATE tSET t.Bundle=CASE WHEN (CAST(b.BundleCountAs INT)%8)>0 THEN (b.BundleCount/8)+ 1 ELSE (b.BundleCount/8) ENDFROM YourTable tCROSS APPLY (SELECT SUM(Thickness) AS BundleCount FROM Table WHERE Sack=t.Sack AND OrderNumber<=t.OrderNumber)b--Then I set value as a whole number byUpdate TableSet Bundle - Substring(CAST(Bundle as VARCAR), 1, CHARINDEX(CAST(Bundle as VARCAR), 1))) as Float)Thanks again for all you help |
 |
|
|
|
|
|
|
|