473,407 Members | 2,546 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

SQL: Running Sum?

Can anybody point me to an example of creating a running sum via SQL.

The situation is the application shows a history of bond trades - both as parent
transaction and as allocated among funds. The user wants to see the current
total holdings after each trade - both at the parent and fund allocation level.

I wimped out and created a "CurrentNetBalance" field in each of the two tables
and maintained it when processing a trade - having jumped to the conclusion
that recalculating same for every row shown would kill performance.

The obvious has been pointed out to me: that that's a poor solution, laden with
additional maintenance issues when the user wants to delete or retroactively
modify a trade.

I'm pretty sure something like that can be done via SQL on a small dataset
without too much of a performance hit because the SQL below runs quite quickly.
I'd try to adapt that to my needs, but I don't have a clue how or why it
works... and I've also found that it breaks sometimes.

-------------------------------------------------
SELECT (SELECT Count(*) FROM tlkpCountry as xxx
WHERE xxx.Country < tlkpCountry.Country)+1 AS RowNum, tlkpCountry.Country
FROM tlkpCountry;
-------------------------------------------------

So, bottom line, can somebody aim me at an example?
--
PeteCresswell
Oct 26 '06 #1
7 6214
"(PeteCresswell)" <x@y.Invalidwrote in
news:0h********************************@4ax.com:
Can anybody point me to an example of creating a running sum via SQL.

The situation is the application shows a history of bond trades - both
as parent transaction and as allocated among funds. The user wants
to see the current total holdings after each trade - both at the
parent and fund allocation level.

I wimped out and created a "CurrentNetBalance" field in each of the
two tables and maintained it when processing a trade - having jumped
to the conclusion that recalculating same for every row shown would
kill performance.

The obvious has been pointed out to me: that that's a poor solution,
laden with additional maintenance issues when the user wants to delete
or retroactively modify a trade.

I'm pretty sure something like that can be done via SQL on a small
dataset without too much of a performance hit because the SQL below
runs quite quickly. I'd try to adapt that to my needs, but I don't
have a clue how or why it works... and I've also found that it breaks
sometimes.

-------------------------------------------------
SELECT (SELECT Count(*) FROM tlkpCountry as xxx
WHERE xxx.Country < tlkpCountry.Country)+1 AS RowNum,
tlkpCountry.Country FROM tlkpCountry;
-------------------------------------------------

So, bottom line, can somebody aim me at an example?
Not me ...

We'va had a very long running thread here recently about something
similar; someone wants to update each row, or display for each row, data
calculated on the basis of other rows, usually preceding rows, however
preceding may be defined.

IMO SQL was not designed with this capability in mind. What are the
solutions?

1. Abuse the SQL so that it is recursive and calculates for each row the
required summmative data for all preceding rows as per your example; as
the number of rows increases this gets slower and slower as it requires
recalculation on something like n(n+1)/2 rows;
2. Open a recordset and scan through it, updating a summative field or
fields; this is as current as loading the dataset, which is often not
current enough, especially if the scan is not instantaneous; but a scan
is a scan, it's not a repetitive scan, that is row 1 is or should be
looked at once and once only; some people think that disconnecting a
recordset makes this solution more digestible;
3. Display the data in a report and use the report's running sum
capbilities;
4. If we are using MS-SQL or similar powerful engine we can hide all of 1
above in a Stored Procedure or UDF as well as do it more efficiently; all
the work will be done on the server; many say this is a good thing; I am
not so sure; while your system is idle do you want the server's system to
be doing the work of 100 users like you?
5. I don't know if cross tabs are a solution to this;
6. Combine the notion of opening a recordset with the idea of display;
this is done regularly in html display of data and can be done in Access
by setting the form's (or in Access 2003 ADPs report's) recordset to the
opened recordset(ADO). Of course, we can display html in Access if we are
willing to use the WebBrowser Control.

Are there other solutions? I would be very happy to be reminded of them
or to be instructed in them.

IMO the report is the preferred solution in most cases. Reports have very
powerful grouping and summative capabilities. And the client sees but
cannot mess with the results.

--
Lyle Fairfield
Oct 26 '06 #2
Per Lyle Fairfield:
>1. Abuse the SQL so that it is recursive and calculates for each row the
required summmative data for all preceding rows as per your example; as
the number of rows increases this gets slower and slower as it requires
recalculation on something like n(n+1)/2 rows;
Thanks for taking the time.

That was my concern - and the reason I posted instead of just going with what
Google came up with... as in
http://www.databasejournal.com/featu...le.php/3112381
--
PeteCresswell
Oct 26 '06 #3
Per Lyle Fairfield:
>Are there other solutions? I would be very happy to be reminded of them
or to be instructed in them.
The one I came up with (denormalizing the totals and maintaining them) isn't
looking as bad now as it did last nite.... -)
--
PeteCresswell
Oct 26 '06 #4

(PeteCresswell) wrote:
Per Lyle Fairfield:
Are there other solutions? I would be very happy to be reminded of them
or to be instructed in them.

The one I came up with (denormalizing the totals and maintaining them) isn't
looking as bad now as it did last nite.... -)
--
PeteCresswell
Examples here.

http://search.support.microsoft.com/...7&mode=a&adv=1

Oct 26 '06 #5
Lyle Fairfield wrote:
"(PeteCresswell)" <x@y.Invalidwrote in
news:0h********************************@4ax.com:
Can anybody point me to an example of creating a running sum via SQL.
4. If we are using MS-SQL or similar powerful engine we can hide all of 1
above in a Stored Procedure or UDF as well as do it more efficiently; all
the work will be done on the server; many say this is a good thing; I am
not so sure; while your system is idle do you want the server's system to
be doing the work of 100 users like you?
Lyle Fairfield
You've stated what I've only thought in the past. Normally it's a
no-brainer that distributing the processing out to the "client side"
has many advantages over loading up the server. There are a couple of
other things of which you need to be aware. Both Microsoft and Intel
are saying that because of the problem of cooling CPU's, the trend is
about to turn sharply in the direction of more and more cores (think 20
to 40 sometime soon). So is it better to have all that wasted latency
on the server or on the client machine? Server software is going to be
the first to take full advantage of the parallel capabilities of the
multi-core architecture, but later the client machine software will
also be able to take proper advantage of its own multi-core
capabilities. I don't disagree with your point, I'm simply saying that
the decision is not as much of a no-brainer as it seems to be on the
surface.

BTW, I came up with a couple of different ways of getting a quickly
executing running sum in a query using functions (one used a static
variable). This technique worked properly except for the reevaluation
when the query result window was changed so I dumped that idea. As you
say, the fact that reports don't have that limitation makes them a
candidate for running sums. One interesting result is that a UDF
inside a query is called once prior to it's first use within the query.

James A. Fortune
CD********@FortuneJames.com

Oct 30 '06 #6

"(PeteCresswell)" <x@y.Invalidschreef in bericht news:0h********************************@4ax.com...

I'm pretty sure something like that can be done via SQL on a small dataset
without too much of a performance hit because the SQL below runs quite quickly.
I'd try to adapt that to my needs, but I don't have a clue how or why it
works... and I've also found that it breaks sometimes.

-------------------------------------------------
SELECT (SELECT Count(*) FROM tlkpCountry as xxx
WHERE xxx.Country < tlkpCountry.Country)+1 AS RowNum, tlkpCountry.Country
FROM tlkpCountry;
-------------------------------------------------

So, bottom line, can somebody aim me at an example?
--
PeteCresswell
You might want to take a look at this:
http://www.fabalou.com/access/Querie...TotalQuery.asp

and this
http://www.fabalou.com/Access/Queries/querycreator.asp
This add-in is available since the Access 2.0 days if I recall correctly.

Arno R
Oct 30 '06 #7
CD********@FortuneJames.com wrote:
Both Microsoft and Intel
are saying that because of the problem of cooling CPU's, the trend is
about to turn sharply in the direction of more and more cores (think 20
to 40 sometime soon).
See:

http://www.gotw.ca/publications/concurrency-ddj.htm

James A. Fortune
CD********@FortuneJames.com

Dec 8 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Ton | last post by:
I have a question about connectivity between different databases. Oracle, DB2400 and SQL_server. It is easy to integrate application products that are running on different DB platforms? What...
2
by: epaetz | last post by:
I'm getting Not associated with a trusted SQL Server connection errors on a .Net windows service I wrote, when it's running on my application server. It's not a problem with mixed mode...
3
by: Michael | last post by:
Dear All I have problem with my database server which running SQL server 2000. The server running very slow. The worst case, to save a record required more than 20-30 seconds. Since this...
12
by: RKay | last post by:
I have a Win2k server running SQL Server 2000. On that box I built a working web service that pulls data from the database. One of the services available simply accepts an ado.net connection string...
2
by: Dave Hughes | last post by:
Just noticed something rather annoying after upgrading my test box (a Linux server running DB2 UDB v8 for LUW) to fixpak 11 (for reference it was previously on fixpak 7). In the past I've relied...
5
by: Jack | last post by:
Hello, I had SQL2000 server running fine on the windows 2003 box. I then installed SQL 2005 Express. They both worked together fine. I then uninstalled SQL 2005 Express and installed SQL 2005...
14
by: Developer | last post by:
Hello All, i have recently installed VS2005 and was trying to install SQL sever 2000. I have Win XP' SP2. But when I tried installing, it only installed client tools and not the database. Can...
4
by: mike | last post by:
I have found that orig tested 64 bit on our 64bit windows 2003 server...about 1 year ago...and company decided to use sql 32 bit on the 64bit os my question and any information is very welcome ...
6
by: xx75vulcan | last post by:
Hi, I have two seperate production machines that I'm trying to get to play nicely together. This setup has worked for years before - until recently bother machines were reformatted, and...
8
by: Brett | last post by:
I wrote an ASP.NET application that queries a SQL Server database (on a different box from the web server) and displays the result in a GridView. The datasource for the GridView is a SQLDataSource....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.