473,799 Members | 2,723 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 "CurrentNetBala nce" 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.Cou ntry)+1 AS RowNum, tlkpCountry.Cou ntry
FROM tlkpCountry;
-------------------------------------------------

So, bottom line, can somebody aim me at an example?
--
PeteCresswell
Oct 26 '06 #1
7 6229
"(PeteCresswell )" <x@y.Invalidwro te in
news:0h******** *************** *********@4ax.c om:
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 "CurrentNetBala nce" 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.Cou ntry)+1 AS RowNum,
tlkpCountry.Cou ntry 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
recalculatio n 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.Invalidwro te in
news:0h******** *************** *********@4ax.c om:
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********@Fort uneJames.com

Oct 30 '06 #6

"(PeteCresswell )" <x@y.Invalidsch reef in bericht news:0h******** *************** *********@4ax.c om...

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.Cou ntry)+1 AS RowNum, tlkpCountry.Cou ntry
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********@Fort uneJames.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********@Fort uneJames.com

Dec 8 '06 #8

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

Similar topics

1
4044
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 products can be used to achieve this? Our consultant is mentioning .NET but then we have to run everything on SQL server. Currently we are running on Oracle but our third party generic applications are first developed for SQL server and later...
2
2581
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 security. I'm set for mixed mode and I've been running the service on the app server for over a month with no problem. My database is running on a second server and both are under the same domain. The problem has occurred twice over the last two months.
3
12878
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 problem, I usually monitoring Process Info from Enterprise Manager (Management - Current Activity), and I found a misterious process as follow : 1. User : System
12
1314
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 to test if a connection can be made to the local server. I can call that web service from any box on my network, and with the correct connection string, I can establish a connection to the database. However, when I copy the exact same service...
2
4878
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 heavily on the ability to start multiple long-running SQL scripts in the background and have them all run in parallel. Unfortunately, it seems that somewhere between fixpak 7 and fixpak 11 something has been fixed / broken which prevents more...
5
4233
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 Server. But when I open SQL Server Management Studio, I can only connect to the SQL 2000 engine. In the Object explorer, it says v8.0.2039 (which I think is SQL 2000 Server, because I can see the existing SQL 2000 databases). How can I get SQL...
14
3045
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 anyone please help me with this as I want to install SQL server and also wouold be grateful, if you can suggest me any workaround to dealwith this problem.(Like should I install any new OS etc). Any help would be appreciated.
4
5724
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 is there any things in need to know about awe with memory above 4g ...whilst using 32 bit sql on a 64bit os i want to increase the memory on the server but i really don't know what im up against since this is
6
5387
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 reinstalled fresh. I know I'm missing a simple setting here somewhere... I have a Win 2003 IIS 6.0 box running as web server, and a Win 2003 IIS 6.0 box running as a web server with SQL 2000 Server installed.
8
6340
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. Just to get it to work, I hard-coded the username and password of a SQL Server account in the connectionstring in web.config. Once I confirmed that this worked on the web server, I wanted to remove the hard-coded password from web.config, so I...
0
9687
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9543
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10488
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10257
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7567
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5467
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5588
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4144
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2941
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.