dbTalk Databases Forums  

Sum Accumulating Data

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss Sum Accumulating Data in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
sucaba.r@gmail.com
 
Posts: n/a

Default Sum Accumulating Data - 04-14-2006 , 09:05 AM






I've got a table that contains a column of accumulating uptime data
that looks similar to this:

239.13
239.21
239.30
239.38
239.46
239.55
0.35
0.44
0.53
0.60
0.68
0.78
0.85
0.93

I need to SUM the data up the point where the data gets reset (the next
row is less than the preceeding row). Then I start the SUM again until
data gets reset.

Thanks in advance for help!


Reply With Quote
  #2  
Old   
Tom Moreau
 
Posts: n/a

Default Re: Sum Accumulating Data - 04-14-2006 , 09:23 AM






Without a primary key, little can be done. The data aren't stored in any
particular order, so as far as SQL Server is concerned, the list of values
you have given us is no different from

0.35
0.44
0.53
0.60
0.68
0.78
0.85
0.93
239.13
239.21
239.30
239.38
239.46
239.55


--
Tom

----------------------------------------------------
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
SQL Server MVP
Toronto, ON Canada
..
<sucaba.r (AT) gmail (DOT) com> wrote

I've got a table that contains a column of accumulating uptime data
that looks similar to this:

239.13
239.21
239.30
239.38
239.46
239.55
0.35
0.44
0.53
0.60
0.68
0.78
0.85
0.93

I need to SUM the data up the point where the data gets reset (the next
row is less than the preceeding row). Then I start the SUM again until
data gets reset.

Thanks in advance for help!


Reply With Quote
  #3  
Old   
sucaba.r@gmail.com
 
Posts: n/a

Default Re: Sum Accumulating Data - 04-14-2006 , 02:10 PM



Sorry, there is a primary key, just didn't show it.


Reply With Quote
  #4  
Old   
Tom Moreau
 
Posts: n/a

Default Re: Sum Accumulating Data - 04-14-2006 , 02:12 PM



DO you also have date/time information? What determines the actual sequence
of the data? Without posting the DDL, we are left to guess.

--
Tom

----------------------------------------------------
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
SQL Server MVP
Toronto, ON Canada
..
<sucaba.r (AT) gmail (DOT) com> wrote

Sorry, there is a primary key, just didn't show it.


Reply With Quote
  #5  
Old   
Erland Sommarskog
 
Posts: n/a

Default Re: Sum Accumulating Data - 04-14-2006 , 04:42 PM



(sucaba.r (AT) gmail (DOT) com) writes:
Quote:
I've got a table that contains a column of accumulating uptime data
that looks similar to this:

239.13
239.21
239.30
239.38
239.46
239.55
0.35
0.44
0.53
0.60
0.68
0.78
0.85
0.93

I need to SUM the data up the point where the data gets reset (the next
row is less than the preceeding row). Then I start the SUM again until
data gets reset.
You want something like this:

239,13
239,21 478,34
239,3 717,64
239,38 957,02
239,46 1196,48
239,55 1436,03
0,35 0,35
0,44 0,79
0,53 1,32
0,6 1,92
0,68 2,6
0,78 3,38
0,85 4,23
0,93 5,16

If the values are accumulated sums, it seems funny to sum them again.

Since I don't want to solve the wrong problem, I suggest that you post

o The CREATE TABLE statement for your table (possibly simplified).
o INSERT statements with sample data.
o The desired output given the sample.

That is likely to give you a tested solution. Whereas from what you
have posted now, at best could give you guesses.


--
Erland Sommarskog, SQL Server MVP, esquel (AT) sommarskog (DOT) se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx


Reply With Quote
  #6  
Old   
Greg D. Moore \(Strider\)
 
Posts: n/a

Default Re: Sum Accumulating Data - 04-14-2006 , 06:15 PM




<sucaba.r (AT) gmail (DOT) com> wrote

Quote:
I've got a table that contains a column of accumulating uptime data
that looks similar to this:

239.13
239.21
239.30
239.38
239.46
239.55
0.35
0.44
0.53
0.60
0.68
0.78
0.85
0.93
As others have suggested, DDL will help.

However, based on what I just saw for row_numbers in SQL 2005, this may be a
great application for their use.

So, what version of SQL are you running?


Quote:
I need to SUM the data up the point where the data gets reset (the next
row is less than the preceeding row). Then I start the SUM again until
data gets reset.

Thanks in advance for help!




Reply With Quote
  #7  
Old   
sucaba.r@gmail.com
 
Posts: n/a

Default Re: Sum Accumulating Data - 04-18-2006 , 10:42 AM



Sorry for the confusion guys. I was misinformed about what I was
looking for in the table. Given the same type of data:

1 - 239.21
2 - 239.55
3 - 240.30
4 - 0.35
5 - 0.44
6 - 0.53
7 - 1.20
8 - 2.40
9 - 0.25

I need to know how many rows before the following row is less than the
previous (in this example, a total of 5). I'm trying to find the number
of entries (could be days, weeks) before a computer is reset, and SUM
them for a total number of occurences (days, weeks, whatever). So for
this I'd sum rows 1,2,3,7,8 for a total of 5.
Does this help?

Thanks again.


Reply With Quote
  #8  
Old   
Tom Moreau
 
Posts: n/a

Default Re: Sum Accumulating Data - 04-18-2006 , 11:47 AM



Your spec is both confusing and inconsistent. The groups your data have
show progressions of:

3
5
1

The following code will show that:

declare @t table
(
PK int primary key
, Amount dec (6, 2) not null
)

insert @t values (1, 239.21)
insert @t values (2, 239.55)
insert @t values (3, 240.30)
insert @t values (4, 0.35)
insert @t values (5, 0.44)
insert @t values (6, 0.53)
insert @t values (7, 1.20)
insert @t values (8, 2.40)
insert @t values (9, 0.25)

select
FromPK
, count (*)
from
(
select
(
select top 1
b.PK
from
@t b
where
b.PK <= a.PK
and b.Amount <= a.Amount
order by
b.PK, b.Amount desc

) FromPK
from
@t a
) x
group by
FromPK

However, you seem to think that the progressions are:

3
2

Why is that?

--
Tom

----------------------------------------------------
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
SQL Server MVP
Toronto, ON Canada
..
<sucaba.r (AT) gmail (DOT) com> wrote

Sorry for the confusion guys. I was misinformed about what I was
looking for in the table. Given the same type of data:

1 - 239.21
2 - 239.55
3 - 240.30
4 - 0.35
5 - 0.44
6 - 0.53
7 - 1.20
8 - 2.40
9 - 0.25

I need to know how many rows before the following row is less than the
previous (in this example, a total of 5). I'm trying to find the number
of entries (could be days, weeks) before a computer is reset, and SUM
them for a total number of occurences (days, weeks, whatever). So for
this I'd sum rows 1,2,3,7,8 for a total of 5.
Does this help?

Thanks again.


Reply With Quote
  #9  
Old   
sucaba.r@gmail.com
 
Posts: n/a

Default Re: Sum Accumulating Data - 04-19-2006 , 07:01 AM



Hi Tom,

Quote:
From looking at the table, the system ran for three days before it was
reset (rows 1-3). It then ran for 4 days (rows 4-7) for a total of 7
(not 5 as I indicated above). So I'd need to capture 7 as the total.

Thanks



Reply With Quote
  #10  
Old   
Tom Moreau
 
Posts: n/a

Default Re: Sum Accumulating Data - 04-19-2006 , 04:34 PM



So, does my code meet the requirement?

--
Tom

----------------------------------------------------
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
SQL Server MVP
Toronto, ON Canada
..
<sucaba.r (AT) gmail (DOT) com> wrote

Hi Tom,

Quote:
From looking at the table, the system ran for three days before it was
reset (rows 1-3). It then ran for 4 days (rows 4-7) for a total of 7
(not 5 as I indicated above). So I'd need to capture 7 as the total.

Thanks



Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.