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.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Particular query with comparison

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)
Go
Insert 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 - Value
6 - 11
11 - 31

How 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)
Go
Insert 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 - Value
6 - 11
11 - 31

How can I solve this particular problem?
(I have to use only SQL Server 2000 features)

Thanks a lot.

Luigi

Go to Top of Page

Ciupaz
Posting Yak Master

232 Posts

Posted - 2010-04-23 : 10:55:52
Yes, you'are right.

Luigi
Go to Top of Page

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

Go to Top of Page

hanbingl
Aged Yak Warrior

652 Posts

Posted - 2010-04-23 : 11:21:58
[code]
declare @i int
declare @sumval int
declare @curmonth int
set @curmonth = 0
set @sumval = 0
set @i = 1
while @i <= 12
begin
select @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 = @i

if (@curmonth <> 0 and @curmonth <> @i)
begin
INSERT INTO #reportinsert values(@curmonth+1, @sumval)
set @curmonth = 0
set @sumval = 0
end

print @i
print @curmonth
print @sumval
print '---------------'
set @i=@i+1

end

select * from #reportinsert
[/code]

quote:
Originally posted by Ciupaz

Yes, you'are right.

Luigi

Go to Top of Page

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



Go to Top of Page

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





Go to Top of Page

hanbingl
Aged Yak Warrior

652 Posts

Posted - 2010-04-23 : 16:00:12
[code]
declare @i int
declare @sumval int
declare @curmonth int
declare @firstzero int
set @firstzero = 0
set @curmonth = 0
set @sumval = 0
set @i = 1
while @i <= 12
begin
select @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 = @i
if (@firstzero = 0 and @curmonth > 0)
set @firstzero = @curmonth
if (@curmonth = 12)
begin
INSERT INTO #reportinsert values(@firstzero-1, @sumval)
break
end
if (@curmonth <> 0 and @curmonth <> @i)
begin
INSERT INTO #reportinsert values(@curmonth+1, @sumval)
set @curmonth = 0
set @sumval = 0
set @firstzero = 0
end

print @i
print @curmonth
print @sumval
print @firstzero
print '-------------'

set @i=@i+1

end
[/code]
Go to Top of Page

hanbingl
Aged Yak Warrior

652 Posts

Posted - 2010-04-23 : 16:04:31
Another question:

Say if you have this:

Month Value
----------- -----------------------------------------------------
1 16.0
2 11.0
3 12.0
4 15.0
5 0.0
6 34.0
7 56.0
8 0.0
9 24.0
10 0.0
11 0.0
12 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?
Go to Top of Page

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 MVP
http://visakhm.blogspot.com/

Go to Top of Page

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) -> 105

Month - Value
9 - 105

PS
I have to check this particular case with the analyst however.
Go to Top of Page

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 MVP
http://visakhm.blogspot.com/



Go to Top of Page

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) -> 105

Month - Value
9 - 105

PS
I have to check this particular case with the analyst however.


Go to Top of Page

hanbingl
Aged Yak Warrior

652 Posts

Posted - 2010-04-23 : 16:42:55
btw, here is my answer, adjust if you need.


declare @i int
declare @sumval int
declare @curmonth int
declare @firstzero int
set @firstzero = 0
set @curmonth = 0
set @sumval = 0
set @i = 1
while @i <= 12
begin
select @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 = @i
if (@firstzero = 0 and @curmonth > 0)
set @firstzero = @curmonth
if (@curmonth = 12)
begin
if exists (select 1 from #reportinsert where month = @firstzero-1)
UPDATE #reportinsert set value = value+@sumval where month = @firstzero-1
ELSE
INSERT INTO #reportinsert values(@firstzero-1, @sumval)
break
end
if (@curmonth <> 0 and @curmonth <> @i)
begin
INSERT INTO #reportinsert values(@curmonth+1, @sumval)
set @curmonth = 0
set @sumval = 0
set @firstzero = 0
end

print @i
print @curmonth
print @sumval
print @firstzero
print '-------------'

set @i=@i+1

end
Go to Top of Page

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
Go to Top of Page

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 - Value
1 16
2 11
3 12
4 15
5 0
6 11
7 56
8 14
9 24
10 10
11 12
12 0


Table ReportCorrect

Month - Value
1 16
2 11,6
3 12
4 15,5
5 11
6 34
7 56,5
8 14
9 24,2
10 31
11 12
12 14

I obtain

Month Value
3 11
8 116
11 69

instead of:

Month - Value
6 - 45
11 - 26

Go to Top of Page

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)
Go
Create Table ReportCorrect(Month int, Value float)
Go
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)
Go
Insert 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 Unit

with these values:

Insert Percentage values (1,1,1,20) -- Percentage for UP=1, Fuel=1, January
Insert Percentage values (1,2,1,25) -- Percentage for UP=1, Fuel=2, January
Insert Percentage values (2,1,1,15) -- Percentage for UP=2, Fuel=1, January
Insert Percentage values (2,2,1,35) -- Percentage for UP=2, Fuel=2, January
go
Insert Percentage values (1,1,2,20) -- Percentage for UP=1, Fuel=1, February
Insert Percentage values (1,2,2,25) -- Percentage for UP=1, Fuel=2, February
Insert Percentage values (2,1,2,18) -- Percentage for UP=2, Fuel=1, February
Insert Percentage values (2,2,2,25) -- Percentage for UP=2, Fuel=2, February
go
Insert Percentage values (1,1,3,33) -- Percentage for UP=1, Fuel=1, March
Insert Percentage values (1,2,3,15) -- Percentage for UP=1, Fuel=2, March
Insert Percentage values (2,1,3,25) -- Percentage for UP=2, Fuel=1, March
Insert Percentage values (2,2,3,17) -- Percentage for UP=2, Fuel=2, March
go
Insert Percentage values (1,1,4,26) -- Percentage for UP=1, Fuel=1, April
Insert Percentage values (1,2,4,25) -- Percentage for UP=1, Fuel=2, April
Insert Percentage values (2,1,4,14) -- Percentage for UP=2, Fuel=1, April
Insert Percentage values (2,2,4,32) -- Percentage for UP=2, Fuel=2, April
go
Insert Percentage values (1,1,5,24) -- Percentage for UP=1, Fuel=1, May
Insert Percentage values (1,2,5,25) -- Percentage for UP=1, Fuel=2, May
Insert Percentage values (2,1,5,35) -- Percentage for UP=2, Fuel=1, May
Insert Percentage values (2,2,5,15) -- Percentage for UP=2, Fuel=2, May
go
Insert Percentage values (1,1,6,10) -- Percentage for UP=1, Fuel=1, June
Insert Percentage values (1,2,6,26) -- Percentage for UP=1, Fuel=2, June
Insert Percentage values (2,1,6,25) -- Percentage for UP=2, Fuel=1, June
Insert Percentage values (2,2,6,36) -- Percentage for UP=2, Fuel=2, June
go
Insert Percentage values (1,1,7,24) -- Percentage for UP=1, Fuel=1, July
Insert Percentage values (1,2,7,15) -- Percentage for UP=1, Fuel=2, July
Insert Percentage values (2,1,7,36) -- Percentage for UP=2, Fuel=1, July
Insert Percentage values (2,2,7,50) -- Percentage for UP=2, Fuel=2, July
go
Insert Percentage values (1,1,8,30) -- Percentage for UP=1, Fuel=1, August
Insert Percentage values (1,2,8,16) -- Percentage for UP=1, Fuel=2, August
Insert Percentage values (2,1,8,21) -- Percentage for UP=2, Fuel=1, August
Insert Percentage values (2,2,8,37) -- Percentage for UP=2, Fuel=2, August

etc 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, November
Insert Percentage values (1,2,11,15) -- Percentage for UP=1, Fuel=2, November
Insert Percentage values (2,1,11,25) -- Percentage for UP=2, Fuel=1, November
Insert Percentage values (2,2,11,17) -- Percentage for UP=2, Fuel=2, November


Now, 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<>0
So 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 - Value
1 - 1 - 6 - (11 * 10%)=1.1
1 - 2 - 6 - (11 * 26%)=2.86
2 - 1 - 6 - (11 * 25%)=2.75
2 - 2 - 6 - (11 * 36%)=3.96

then 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 - Value
1 - 1 - 6 - (11 * 10%)=1.1
1 - 2 - 6 - (11 * 26%)=2.86
2 - 1 - 6 - (11 * 25%)=2.75
2 - 2 - 6 - (11 * 36%)=3.96

1 - 1 - 7 - (34 * 24%)=8.16
1 - 2 - 7 - (34 * 15%)=5.1
2 - 1 - 7 - (34 * 36%)=12.24
2 - 2 - 7 - (34 * 50%)=17


How can I solve this very complicated calculation?
Go to Top of Page

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 float
set @i = 1

while @i <= 12
begin

select @Value = Value from dbo.Report where [MONTH] = @i
if @Value <> 0
begin
set @i = @i + 1
continue;
end

else
begin
select @FollowingValue = Value from dbo.Report where [MONTH] = @i+1
if @FollowingValue <> 0
begin
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] = @i
end

else -- Also following month has value=0
begin
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
continue
end
end
set @i = @i + 1
end


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.

Go to Top of Page

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 > 0

quote:
Originally posted by Ciupaz

There are some problems. For example, in this case:

Table Report

Month - Value
1 16
2 11
3 12
4 15
5 0
6 11
7 56
8 14
9 24
10 10
11 12
12 0


Table ReportCorrect

Month - Value
1 16
2 11,6
3 12
4 15,5
5 11
6 34
7 56,5
8 14
9 24,2
10 31
11 12
12 14

I obtain

Month Value
3 11
8 116
11 69

instead of:

Month - Value
6 - 45
11 - 26



Go to Top of Page

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)
go
create table dbo. ReportComplete (month int, float $ value)
go
--We have two UP (770 and 771) and two fuels (1 and 2)
--January
insert dbo. Report values (770,1, 1, 12.4), (770,2, 1, 12.6), (771,1, 1, 2, 3.5), (771,2, 1, 5.5)
--February
insert dbo. Report values (770,1, 2, 12.4), (770,2, 2, 12.6), (771,1, 2, 3.5), (771,2, 2, 5.5)
--March
insert dbo. Report values (770,1, 3, 12.4), (770,2, 3, 12.6), (771,1, 3, 3.5), (771,2, 3, 5.5)
--April
insert 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)
--May
insert 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)
--June
insert dbo. Report values (770,1, 6, 12.4), (770,2, 6, 12.6), (771,1, 6, 2, 3.5), (771,2, 6, 5.5)
--July
insert dbo. Report values (770,1, 7, 12.4), (770,2, 7, 12.6), (771,1, 7, 2, 3.5), (771,2, 7, 5.5)
--August
insert 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)
--September
insert dbo. Report values (770,1, 9, 12.4), (770,2, 9, 12.6), (771,1, 9, 2, 3.5), (771,2, 9, 5.5)
--October
insert dbo. Report values (770,1, 10, 0), (770,2, 10, 0), (771,1, 10, 0), (771,2, 10, 0)
--November
insert dbo. Report values (770,1, 11, 12.4), (770,2, 11, 12.6), (771,1, 11, 2, 3.5), (771,2, 11, 5.5)
--December
insert dbo. Report values (770,1, 12, 12.4), (770,2, 12, 12.6), (771,1, 12, 2, 3.5), (771,2, 12, 5.5)
go

insert 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-Value
770-1-1-8.6


Do you have any idea how to solve?

Luigi
Go to Top of Page
   

- Advertisement -