| Author |
Topic |
|
Ciupaz
Posting Yak Master
232 Posts |
Posted - 2010-04-23 : 10:37:46
|
| Hi,I have 2 similar tables, like these:Create Table Report(Month int, Value float)Create Table ReportCorrect(Month int, Value float)with these values:Insert Report values(1,16),(2,11),(3,12),(4,15),(5,0),(6,34),(7,56),(8,14),(9,24),(10,0),(11,12),(12,14)GoInsert ReportCorrect values(1,16),(2,11),(3,12),(4,15),(5,11),(6,34),(7,56),(8,14),(9,24),(10,31),(11,12),(12,14)In the Report table, for the months 5 (May) and 10 (October), I have Value=0.Now I have to insert this gap (11 and 31) in another table (with the same structure), only if the following month has not zero value. If has zero value, I have to sum these values and put in the following month, and so on. In this case I should obtain Month - Value6 - 1111 - 31How can I solve this particular problem? (I have to use only SQL Server 2000 features)Thanks a lot.Luigi |
|
|
hanbingl
Aged Yak Warrior
652 Posts |
Posted - 2010-04-23 : 10:52:42
|
so in your case if the following month (6,34)in report is (6,0), you have to insert as 11+34 (7,45) ?quote: Originally posted by Ciupaz Hi,I have 2 similar tables, like these:Create Table Report(Month int, Value float)Create Table ReportCorrect(Month int, Value float)with these values:Insert Report values(1,16),(2,11),(3,12),(4,15),(5,0),(6,34),(7,56),(8,14),(9,24),(10,0),(11,12),(12,14)GoInsert ReportCorrect values(1,16),(2,11),(3,12),(4,15),(5,11),(6,34),(7,56),(8,14),(9,24),(10,31),(11,12),(12,14)In the Report table, for the months 5 (May) and 10 (October), I have Value=0.Now I have to insert this gap (11 and 31) in another table (with the same structure), only if the following month has not zero value. If has zero value, I have to sum these values and put in the following month, and so on. In this case I should obtain Month - Value6 - 1111 - 31How can I solve this particular problem? (I have to use only SQL Server 2000 features)Thanks a lot.Luigi
|
 |
|
|
Ciupaz
Posting Yak Master
232 Posts |
Posted - 2010-04-23 : 10:55:52
|
| Yes, you'are right. Luigi |
 |
|
|
hanbingl
Aged Yak Warrior
652 Posts |
Posted - 2010-04-23 : 11:20:22
|
What if month 12 has 0 value? do you insert to month 1?quote: Originally posted by Ciupaz Yes, you'are right. Luigi
|
 |
|
|
hanbingl
Aged Yak Warrior
652 Posts |
Posted - 2010-04-23 : 11:21:58
|
[code]declare @i intdeclare @sumval intdeclare @curmonth intset @curmonth = 0set @sumval = 0set @i = 1while @i <= 12beginselect @curmonth = a.month, @sumval = @sumval + b.value from #report a join #reportcorrect b on a.month = b.month and a.value <> b.value where a.month = @iif (@curmonth <> 0 and @curmonth <> @i)beginINSERT INTO #reportinsert values(@curmonth+1, @sumval)set @curmonth = 0set @sumval = 0endprint @i print @curmonth print @sumvalprint '---------------'set @i=@i+1endselect * from #reportinsert[/code]quote: Originally posted by Ciupaz Yes, you'are right. Luigi
|
 |
|
|
Ciupaz
Posting Yak Master
232 Posts |
Posted - 2010-04-23 : 15:02:27
|
If December has zero value, I have to put the value in Novembere, if this month has a value <> 0. If also November has zero value, I put them in October, and so on.quote: Originally posted by hanbingl What if month 12 has 0 value? do you insert to month 1?quote: Originally posted by Ciupaz Yes, you'are right. Luigi
|
 |
|
|
hanbingl
Aged Yak Warrior
652 Posts |
Posted - 2010-04-23 : 15:21:06
|
wow, this stinks.quote: Originally posted by Ciupaz If December has zero value, I have to put the value in Novembere, if this month has a value <> 0. If also November has zero value, I put them in October, and so on.quote: Originally posted by hanbingl What if month 12 has 0 value? do you insert to month 1?quote: Originally posted by Ciupaz Yes, you'are right. Luigi
|
 |
|
|
hanbingl
Aged Yak Warrior
652 Posts |
Posted - 2010-04-23 : 16:00:12
|
| [code]declare @i intdeclare @sumval intdeclare @curmonth intdeclare @firstzero intset @firstzero = 0set @curmonth = 0set @sumval = 0set @i = 1while @i <= 12beginselect @curmonth = a.month, @sumval = @sumval + b.value from #report a join #reportcorrect b on a.month = b.month and a.value <> b.value where a.month = @iif (@firstzero = 0 and @curmonth > 0)set @firstzero = @curmonthif (@curmonth = 12) beginINSERT INTO #reportinsert values(@firstzero-1, @sumval)breakendif (@curmonth <> 0 and @curmonth <> @i)beginINSERT INTO #reportinsert values(@curmonth+1, @sumval)set @curmonth = 0set @sumval = 0set @firstzero = 0endprint @i print @curmonthprint @sumvalprint @firstzeroprint '-------------'set @i=@i+1end[/code] |
 |
|
|
hanbingl
Aged Yak Warrior
652 Posts |
Posted - 2010-04-23 : 16:04:31
|
Another question:Say if you have this:Month Value ----------- ----------------------------------------------------- 1 16.02 11.03 12.04 15.05 0.06 34.07 56.08 0.09 24.010 0.011 0.012 0.0 Month 9 will get updated by value of Month 8 and sum of values of 10-12. There is a conflict. What do you want to do in this case? |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-04-23 : 16:11:42
|
| you need to state all your set of rules for somebody to suggest you an accurate solution------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
Ciupaz
Posting Yak Master
232 Posts |
Posted - 2010-04-23 : 16:17:39
|
| Uhm, interesting case...So, in this case September will became:24 + 31(October) + 12(November) + 14(December) + 14(August) -> 105Month - Value9 - 105PSI have to check this particular case with the analyst however. |
 |
|
|
Ciupaz
Posting Yak Master
232 Posts |
Posted - 2010-04-23 : 16:18:53
|
Yes, you're right, sorry for that.quote: Originally posted by visakh16 you need to state all your set of rules for somebody to suggest you an accurate solution------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
|
 |
|
|
hanbingl
Aged Yak Warrior
652 Posts |
Posted - 2010-04-23 : 16:31:01
|
Why +24 (Month 9's correct value) when you do not add the other current month values from the previous conditions?? also your math is 10 off.quote: Originally posted by Ciupaz Uhm, interesting case...So, in this case September will became:24 + 31(October) + 12(November) + 14(December) + 14(August) -> 105Month - Value9 - 105PSI have to check this particular case with the analyst however.
|
 |
|
|
hanbingl
Aged Yak Warrior
652 Posts |
Posted - 2010-04-23 : 16:42:55
|
btw, here is my answer, adjust if you need. declare @i intdeclare @sumval intdeclare @curmonth intdeclare @firstzero intset @firstzero = 0set @curmonth = 0set @sumval = 0set @i = 1while @i <= 12beginselect @curmonth = a.month, @sumval = @sumval + b.value from #report a join #reportcorrect b on a.month = b.month and a.value <> b.value where a.month = @iif (@firstzero = 0 and @curmonth > 0)set @firstzero = @curmonthif (@curmonth = 12) beginif exists (select 1 from #reportinsert where month = @firstzero-1)UPDATE #reportinsert set value = value+@sumval where month = @firstzero-1ELSEINSERT INTO #reportinsert values(@firstzero-1, @sumval)breakendif (@curmonth <> 0 and @curmonth <> @i)beginINSERT INTO #reportinsert values(@curmonth+1, @sumval)set @curmonth = 0set @sumval = 0set @firstzero = 0endprint @i print @curmonthprint @sumvalprint @firstzeroprint '-------------'set @i=@i+1end |
 |
|
|
Ciupaz
Posting Yak Master
232 Posts |
Posted - 2010-04-24 : 01:58:00
|
| 95 sorry (not 105), because September takes the values from October+November+December, plus again August (August is zero).I'm testing your solution. Thanks very much for now.Luigi |
 |
|
|
Ciupaz
Posting Yak Master
232 Posts |
Posted - 2010-04-24 : 05:53:22
|
| There are some problems. For example, in this case:Table Report Month - Value1 162 113 124 155 06 117 568 149 2410 1011 1212 0Table ReportCorrectMonth - Value1 162 11,63 124 15,55 116 347 56,58 149 24,210 3111 1212 14I obtainMonth Value3 118 11611 69instead of:Month - Value6 - 4511 - 26 |
 |
|
|
Ciupaz
Posting Yak Master
232 Posts |
Posted - 2010-04-24 : 08:57:41
|
| A similar question about these "Report" tables.I have again the 2 Report tables:Create Table Report(Month int, Value float)GoCreate Table ReportCorrect(Month int, Value float)GoInsert Report values(1,16),(2,11),(3,12),(4,15),(5,0),(6,34),(7,56),(8,14),(9,24),(10,0),(11,12),(12,14)GoInsert ReportCorrect values(1,16),(2,11),(3,12),(4,15),(5,11),(6,34),(7,56),(8,14),(9,24),(10,31),(11,12),(12,14)Again, in Report table, May and October has Value=0. Now there is a third table, Percentage, that has this structure: Create Table Percentage (UP int, Fuel int, Month int, Percentage float) -- UP -> Production Unitwith these values:Insert Percentage values (1,1,1,20) -- Percentage for UP=1, Fuel=1, JanuaryInsert Percentage values (1,2,1,25) -- Percentage for UP=1, Fuel=2, JanuaryInsert Percentage values (2,1,1,15) -- Percentage for UP=2, Fuel=1, JanuaryInsert Percentage values (2,2,1,35) -- Percentage for UP=2, Fuel=2, JanuarygoInsert Percentage values (1,1,2,20) -- Percentage for UP=1, Fuel=1, FebruaryInsert Percentage values (1,2,2,25) -- Percentage for UP=1, Fuel=2, FebruaryInsert Percentage values (2,1,2,18) -- Percentage for UP=2, Fuel=1, FebruaryInsert Percentage values (2,2,2,25) -- Percentage for UP=2, Fuel=2, FebruarygoInsert Percentage values (1,1,3,33) -- Percentage for UP=1, Fuel=1, MarchInsert Percentage values (1,2,3,15) -- Percentage for UP=1, Fuel=2, MarchInsert Percentage values (2,1,3,25) -- Percentage for UP=2, Fuel=1, MarchInsert Percentage values (2,2,3,17) -- Percentage for UP=2, Fuel=2, MarchgoInsert Percentage values (1,1,4,26) -- Percentage for UP=1, Fuel=1, AprilInsert Percentage values (1,2,4,25) -- Percentage for UP=1, Fuel=2, AprilInsert Percentage values (2,1,4,14) -- Percentage for UP=2, Fuel=1, AprilInsert Percentage values (2,2,4,32) -- Percentage for UP=2, Fuel=2, AprilgoInsert Percentage values (1,1,5,24) -- Percentage for UP=1, Fuel=1, MayInsert Percentage values (1,2,5,25) -- Percentage for UP=1, Fuel=2, MayInsert Percentage values (2,1,5,35) -- Percentage for UP=2, Fuel=1, MayInsert Percentage values (2,2,5,15) -- Percentage for UP=2, Fuel=2, MaygoInsert Percentage values (1,1,6,10) -- Percentage for UP=1, Fuel=1, JuneInsert Percentage values (1,2,6,26) -- Percentage for UP=1, Fuel=2, JuneInsert Percentage values (2,1,6,25) -- Percentage for UP=2, Fuel=1, JuneInsert Percentage values (2,2,6,36) -- Percentage for UP=2, Fuel=2, JunegoInsert Percentage values (1,1,7,24) -- Percentage for UP=1, Fuel=1, JulyInsert Percentage values (1,2,7,15) -- Percentage for UP=1, Fuel=2, JulyInsert Percentage values (2,1,7,36) -- Percentage for UP=2, Fuel=1, JulyInsert Percentage values (2,2,7,50) -- Percentage for UP=2, Fuel=2, JulygoInsert Percentage values (1,1,8,30) -- Percentage for UP=1, Fuel=1, AugustInsert Percentage values (1,2,8,16) -- Percentage for UP=1, Fuel=2, AugustInsert Percentage values (2,1,8,21) -- Percentage for UP=2, Fuel=1, AugustInsert Percentage values (2,2,8,37) -- Percentage for UP=2, Fuel=2, Augustetc etc ......-- November it's necessary for this example (because October has value=0 in Report table)Insert Percentage values (1,1,11,22) -- Percentage for UP=1, Fuel=1, NovemberInsert Percentage values (1,2,11,15) -- Percentage for UP=1, Fuel=2, NovemberInsert Percentage values (2,1,11,25) -- Percentage for UP=2, Fuel=1, NovemberInsert Percentage values (2,2,11,17) -- Percentage for UP=2, Fuel=2, NovemberNow, with the same logic as seen before, I have to perform these calculation:In words: May has value=0 in Report table, and June has value<>0So I have to take the value of May in ReportCorrect table (=11) and create 4 new records (for June) made in this way:UP - Fuel - Month - Value1 - 1 - 6 - (11 * 10%)=1.11 - 2 - 6 - (11 * 26%)=2.862 - 1 - 6 - (11 * 25%)=2.752 - 2 - 6 - (11 * 36%)=3.96then these 4 records I'll put in another table with these four fields. Like before, if also June has value=0 (and July has value <>0), I have to sum (May+June, from ReportCorrect table), and create 8 records:UP - Fuel - Month - Value1 - 1 - 6 - (11 * 10%)=1.11 - 2 - 6 - (11 * 26%)=2.862 - 1 - 6 - (11 * 25%)=2.752 - 2 - 6 - (11 * 36%)=3.961 - 1 - 7 - (34 * 24%)=8.161 - 2 - 7 - (34 * 15%)=5.12 - 1 - 7 - (34 * 36%)=12.242 - 2 - 7 - (34 * 50%)=17How can I solve this very complicated calculation? |
 |
|
|
Ciupaz
Posting Yak Master
232 Posts |
Posted - 2010-04-25 : 15:45:19
|
| I write a partial solution, this one:declare @i int, @FollowingMonth int, @Value float, @ValueCorrect float, @FollowingValue floatset @i = 1while @i <= 12begin select @Value = Value from dbo.Report where [MONTH] = @iif @Value <> 0 beginset @i = @i + 1continue;endelse beginselect @FollowingValue = Value from dbo.Report where [MONTH] = @i+1if @FollowingValue <> 0begin select @ValueCorrect = Value from dbo.ReportCorrect where [MONTH] = @i print cast(@valueCorrect as varchar) insert dbo.ReportNew(UP,Fuel,[Month],Value) select UP, Fuel, @i+1, @ValueCorrect * Percentage/100 from dbo.Percentage where [MONTH] = @iendelse -- Also following month has value=0begin declare @ValueCorrect1 float, @ValueCorrect2 float select @ValueCorrect1 = Value from dbo.ReportCorrect where [MONTH] = @i select @ValueCorrect2 = Value from dbo.ReportCorrect where [MONTH] = @i + 1 --print cast(@valueCorrect as varchar) insert dbo.ReportNew(UP,Fuel,[Month],Value) select UP, Fuel, @i + 2, (@ValueCorrect1 + @ValueCorrect2) * Percentage/100 from dbo.Percentage where [MONTH] = @i + 2 set @i = @i + 2 continueendendset @i = @i + 1end Unfortunately does work in only 2 cases:1) There is one month alone with value=0 (in every position of the year) (for example, May=0 and October=0, but July<>0 and November <>0).2) Two months (the actual and the following). For example May and June has values=0.Does not work if there are 3 or more joined months with value=0 (for example if May=June=July=0) and in the case of December=0, where I have to implement the same mechanism but backward. I find this task excessive complicate to make it in T-SQL. |
 |
|
|
hanbingl
Aged Yak Warrior
652 Posts |
Posted - 2010-04-26 : 10:38:55
|
i assumed if report has value it is same as correct report. Change it to where a.month = b.month and a.value = 0 and b.value > 0quote: Originally posted by Ciupaz There are some problems. For example, in this case:Table Report Month - Value1 162 113 124 155 06 117 568 149 2410 1011 1212 0Table ReportCorrectMonth - Value1 162 11,63 124 15,55 116 347 56,58 149 24,210 3111 1212 14I obtainMonth Value3 118 11611 69instead of:Month - Value6 - 4511 - 26
|
 |
|
|
Ciupaz
Posting Yak Master
232 Posts |
Posted - 2010-04-27 : 05:39:21
|
| Now the problem has been simplified. There are only two tables, with these structures and these test data:Create table dbo. Report (UP Fuel int, int, int, float Value Month)gocreate table dbo. ReportComplete (month int, float $ value)go--We have two UP (770 and 771) and two fuels (1 and 2)--Januaryinsert dbo. Report values (770,1, 1, 12.4), (770,2, 1, 12.6), (771,1, 1, 2, 3.5), (771,2, 1, 5.5)--Februaryinsert dbo. Report values (770,1, 2, 12.4), (770,2, 2, 12.6), (771,1, 2, 3.5), (771,2, 2, 5.5)--Marchinsert dbo. Report values (770,1, 3, 12.4), (770,2, 3, 12.6), (771,1, 3, 3.5), (771,2, 3, 5.5)--Aprilinsert dbo. Report values (770,1, 4, 12.4), (770,2, 4, 12.6), (771,1, 4, 2, 3.5), (771,2 2,3,4,5.5)--Mayinsert dbo. Report values (770,1, 5, 0, 0) 770,2 (6, 5, 0, 0) 771,1 (6, 5, 0, 0) 771,2 (6, 5, 0, 0)--Juneinsert dbo. Report values (770,1, 6, 12.4), (770,2, 6, 12.6), (771,1, 6, 2, 3.5), (771,2, 6, 5.5)--Julyinsert dbo. Report values (770,1, 7, 12.4), (770,2, 7, 12.6), (771,1, 7, 2, 3.5), (771,2, 7, 5.5)--Augustinsert dbo. Report values (770,1, 8, 1: 12.4), (770,2, 8, 12.6), (771,1, 8, 2, 3.5), (771,2, 8, 5.5)--Septemberinsert dbo. Report values (770,1, 9, 12.4), (770,2, 9, 12.6), (771,1, 9, 2, 3.5), (771,2, 9, 5.5)--Octoberinsert dbo. Report values (770,1, 10, 0), (770,2, 10, 0), (771,1, 10, 0), (771,2, 10, 0)--Novemberinsert dbo. Report values (770,1, 11, 12.4), (770,2, 11, 12.6), (771,1, 11, 2, 3.5), (771,2, 11, 5.5)--Decemberinsert dbo. Report values (770,1, 12, 12.4), (770,2, 12, 12.6), (771,1, 12, 2, 3.5), (771,2, 12, 5.5)goinsert dbo. ReportComplete values (1.34), (2: 35), (3.45), (4.34), (5.2.2), (6.45), (7,34), (8.35), (9.45.8), (10.6.4), (11,55), (12,25)I see that in table dbo. Report, for the months of May and October, I have value = 0.I must then take the corresponding Value from the table ReportComplete (2.2 and 6.4) and enter a new record in the table dbo. Report having as the sum of these values (2.2 + 4 = 6.8.6).This new record will have the month the first month of table report where Value <> 0 (in this case, January), and any UP and Fuel (indifferently 770 that 771 and 1 and 2). Practically I should insert a new record like this:Up-Fuel-Month-Value770-1-1-8.6Do you have any idea how to solve? Luigi |
 |
|
|
|
|
|