Thursday, February 16, 2012

Calculating Trends

Hello,
I am looking for the most efficient way to calculate a trend based upon
individual figures in a table. Here is an example (e.g. sold items):
Data:
date number
-- --
2001-01-01 3
2001-01-02 5
2001-01-03 10
expected result:
date totsl number of sold items
-- --
2001-01-01 3
2001-01-02 8
2001-01-03 18
Any idea hoe I can solve that with the best performance possible?
ThanksOn Thu, 2 Jun 2005 22:36:17 +0200, CrazyHorse wrote:

> Hello,
> I am looking for the most efficient way to calculate a trend based upon
> individual figures in a table. Here is an example (e.g. sold items):
> Data:
> date number
> -- --
> 2001-01-01 3
> 2001-01-02 5
> 2001-01-03 10
> expected result:
> date totsl number of sold items
> -- --
> 2001-01-01 3
> 2001-01-02 8
> 2001-01-03 18
> Any idea hoe I can solve that with the best performance possible?
> Thanks
"Calculating Running Totals"
http://www.sqlteam.com/item.asp?ItemID=3856
Summary: Depending on the size of your data, this is one instance where a
cursor may actually be worth using. This is because the cursor will only
make one pass through the table, while any set-based solution has to join
the table with itself - which can produce millions of temporary rows, at
least momentarily.
Most of the time, an even better idea is to let the SQL side just retrieve
the un-summarized data, and let the consuming application produce the
running total. In effect, this is moving the cursor from the database side
to the client side.|||Do:
SELECT dt, ( SELECT SUM( number )
FROM tbl t2
WHERE t2.dt <= t1.dt )
FROM tbl t1 ;
Anith|||Hello,
thanks for your help.
<CrazyHorse> wrote in message news:uvjo$K7ZFHA.2984@.TK2MSFTNGP15.phx.gbl...
> Hello,
> I am looking for the most efficient way to calculate a trend based upon
> individual figures in a table. Here is an example (e.g. sold items):
> Data:
> date number
> -- --
> 2001-01-01 3
> 2001-01-02 5
> 2001-01-03 10
> expected result:
> date totsl number of sold items
> -- --
> 2001-01-01 3
> 2001-01-02 8
> 2001-01-03 18
> Any idea hoe I can solve that with the best performance possible?
> Thanks
>

No comments:

Post a Comment