dbTalk Databases Forums  

TSQL-Accmulations

microsoft.public.sqlserver.clients microsoft.public.sqlserver.clients


Discuss TSQL-Accmulations in the microsoft.public.sqlserver.clients forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Batool
 
Posts: n/a

Default TSQL-Accmulations - 07-01-2007 , 09:45 AM






I need an appropriate select statement for my case, the case is a table with
two columns (row id ,col1) I want to retrieve a three columns
RowId, col1 ,accumulation of col1

I wrote this select statement, but the performance is going down is there an
alternative way for writing this SQL statement

SELECT RowID ,Col1, (SELECT SUM(Col1) from tablehh where RowID <=aa.RowId)
AS Col2 FROM tablehh as aa ORDER BY Col1,Col2
The output should be like this:

RowId Col1 Col2
1 10 10
2 100 110
3 15 125
4 20 145]

Col2 is the accumulation of col1

thx;
batool.




Reply With Quote
  #2  
Old   
JPD
 
Posts: n/a

Default Re: TSQL-Accmulations - 07-19-2007 , 09:35 AM






Hi Batool,

I honestly can't think of a performance-efficient way of doing this.
Any way you try, you are going to incur a huge performance hit as the
number of rows returned by the query increase. SQL Server wasn't
written for this kind of data manipulation.

What you could do is use Excel. Write an MS query that downloads the
non-computed data into an Excel sheet and then have a column in Excel do
the accumulations for you. This way you take the strain off the server
especially if many users need to run this report at the same time.

Jonathan




Batool wrote:
Quote:
I need an appropriate select statement for my case, the case is a table with
two columns (row id ,col1) I want to retrieve a three columns
RowId, col1 ,accumulation of col1

I wrote this select statement, but the performance is going down is there an
alternative way for writing this SQL statement

SELECT RowID ,Col1, (SELECT SUM(Col1) from tablehh where RowID <=aa.RowId)
AS Col2 FROM tablehh as aa ORDER BY Col1,Col2
The output should be like this:

RowId Col1 Col2
1 10 10
2 100 110
3 15 125
4 20 145]

Col2 is the accumulation of col1

thx;
batool.




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.