Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12345»»»

Calculate the Running Total for the last five Transactions Expand / Collapse
Author
Message
Posted Wednesday, November 26, 2008 10:22 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Today @ 4:03 AM
Points: 143, Visits: 549
Comments posted to this topic are about the item Calculate the Running Total for the last five Transactions

--Divya
Post #609561
Posted Wednesday, November 26, 2008 11:53 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 7:58 AM
Points: 23,299, Visits: 32,047
Good article, but let's take another approach and see what we see.

Taking your sample data from your article, and using a slightly different tact, here is another way to tackle your problem given in your article. I have included your sample data, table (with a slight modification for my code), and code as well as my code.

set nocount on
go
CREATE TABLE dbo.Accounts
(
ID int IDENTITY(1,1) primary key, -- Primary Key defaults to a clustered index, needed for this to work
TransactionDate datetime,
Balance money,
RunningBalance money null
);
go
insert into Accounts(TransactionDate,Balance) values ('1/1/2000',100);
insert into Accounts(TransactionDate,Balance) values ('1/2/2000',101);
insert into Accounts(TransactionDate,Balance) values ('1/3/2000',102);
insert into Accounts(TransactionDate,Balance) values ('1/4/2000',103);
insert into Accounts(TransactionDate,Balance) values ('1/5/2000',104);
insert into Accounts(TransactionDate,Balance) values ('1/6/2000',105);
insert into Accounts(TransactionDate,Balance) values ('1/7/2000',106);
insert into Accounts(TransactionDate,Balance) values ('1/8/2000',107);
insert into Accounts(TransactionDate,Balance) values ('1/9/2000',108);
insert into Accounts(TransactionDate,Balance) values ('1/10/2000',109);
insert into Accounts(TransactionDate,Balance) values ('1/11/2000',200);
insert into Accounts(TransactionDate,Balance) values ('1/12/2000',201);
insert into Accounts(TransactionDate,Balance) values ('1/13/2000',202);
insert into Accounts(TransactionDate,Balance) values ('1/14/2000',203);
insert into Accounts(TransactionDate,Balance) values ('1/15/2000',204);
insert into Accounts(TransactionDate,Balance) values ('1/16/2000',205);
insert into Accounts(TransactionDate,Balance) values ('1/17/2000',206);
insert into Accounts(TransactionDate,Balance) values ('1/18/2000',207);
insert into Accounts(TransactionDate,Balance) values ('1/19/2000',208);
insert into Accounts(TransactionDate,Balance) values ('1/20/2000',209);
go
select * from dbo.Accounts;
go
print '-- Cross Join Query --';

set statistics io on

SELECT Acc.ID,CONVERT(varchar(50),TransactionDate,101) AS TransactionDate
, Balance, isnull(RunningTotal,'') AS RunningTotal
FROM Accounts Acc
LEFT OUTER JOIN (SELECT ID,sum(Balance) AS RunningTotal
FROM (SELECT A.ID AS ID,B.ID AS BID, B.Balance
FROM Accounts A
cross JOIN Accounts B
WHERE B.ID BETWEEN A.ID-4
AND A.ID AND A.ID>4)T
GROUP BY ID ) Bal
ON Acc.ID=Bal.ID;

set statistics io off
print '-- Cross Join Query --';
go
print '-- Update Query --';
set statistics io on

declare @var1 money,
@var2 money,
@var3 money,
@var4 money,
@var5 money;

update dbo.Accounts set
@var5 = @var4,
@var4 = @var3,
@var3 = @var2,
@var2 = @var1,
@var1 = Balance,
RunningBalance = isnull(@var1 + @var2 + @var3 + @var4 + @var5, 0);
set statistics io off
print '-- Update Join Query --';
go
print '-- Select After Update Query --';
set statistics io on
select * from dbo.Accounts;
set statistics io off
print '-- Select After Update Query --';
go
drop table dbo.Accounts;
go
set nocount off
go

My results match yours in the article, but what I also wanted to include here were the stats I also had collected using statistic io on (included in the above code).

-- Cross Join Query --
Table 'Accounts'. Scan count 17, logical reads 74, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
-- Cross Join Query --
-- Update Query --
Table 'Accounts'. Scan count 1, logical reads 2, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
-- Update Join Query --
-- Select After Update Query --
Table 'Accounts'. Scan count 1, logical reads 2, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
-- Select After Update Query --


I'll leave further assessment of the different approaches to others. For me, I just wanted to see what might be different.



Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #609582
Posted Thursday, November 27, 2008 12:53 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Thursday, March 6, 2014 6:13 PM
Points: 318, Visits: 1,183
@Lynn:

Gold :)

Regards,

Jacob
Post #609599
Posted Thursday, November 27, 2008 3:12 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Today @ 6:55 AM
Points: 2,366, Visits: 1,845
Brilliant Divya and Lynn

"Keep Trying"
Post #609638
Posted Thursday, November 27, 2008 4:10 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 2:48 AM
Points: 5,977, Visits: 8,238
There is a huge problem with this technique. IDENTITY values are not guaranteed to be consecutive; for a variety of reasons there may be gaps. Those will invalidate the results from this query.

On SQL Server 2005 and above, using ROW_NUMBER() is the adviced technique. On SQL Server 2000 and below - well, let's just say that the required query will be ugly...



Hugo Kornelis, SQL Server MVP
Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
Post #609673
Posted Thursday, November 27, 2008 5:24 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Thursday, August 28, 2014 4:00 AM
Points: 21,397, Visits: 9,612
Hugo, are you talking about the quirky update for 2000?
Post #609715
Posted Thursday, November 27, 2008 5:39 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 2:48 AM
Points: 5,977, Visits: 8,238
Ninja's_RGR'us (11/27/2008)
Hugo, are you talking about the quirky update for 2000?


Hi Ninja's_RGR'us,

No. As far as I know, UPDATE is not quirky at all in SQL Server 2000. What do you mean by "quirky update"?

What I was refering too is the lack of ROW_NUMBER() in SQL Server 2000. This means you'll either have to take your chance with IDENTITY, at the risk of gaps, as the author of this article did; or you have to use a correlated subquery to calculate the row number on the fly, which can result in dramatic performance as the amount of rows grows. Plus, the queries tend to get long and hard to understand.



Hugo Kornelis, SQL Server MVP
Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
Post #609722
Posted Thursday, November 27, 2008 6:00 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Thursday, August 28, 2014 4:00 AM
Points: 21,397, Visits: 9,612
I meant this by quirky update :

Update table set @Var = UpdatedColumn = @Var + WhateverIttakesToEvalutateThisVariable

This must be used with a index hint on a temp table to be sure that nothing goes wrong...

That's a way to make a running total... the case statement here would be quite interesting though ;-P.
Post #609737
Posted Thursday, November 27, 2008 6:10 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 2:48 AM
Points: 5,977, Visits: 8,238
Ninja's_RGR'us (11/27/2008)
I meant this by quirky update :

Update table set @Var = UpdatedColumn = @Var + WhateverIttakesToEvalutateThisVariable

This must be used with a index hint on a temp table to be sure that nothing goes wrong...

That's a way to make a running total... the case statement here would be quite interesting though ;-P.


Oohh, that one.

You know that this "trick" is neither documented nor supported, do you?

(Clarification: "UPDATE ... SET @var = column = expression" is documented and supported, but the effects of the same variable in the expression are not - and given the official intent of an UPDATE statement, the results people currently get in 99% of all cases could be considered a bug).



Hugo Kornelis, SQL Server MVP
Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
Post #609745
Posted Thursday, November 27, 2008 6:16 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Thursday, August 28, 2014 4:00 AM
Points: 21,397, Visits: 9,612
Yup that's the one!

Thanks for the clarifications!
Post #609753
« Prev Topic | Next Topic »

Add to briefcase 12345»»»

Permissions Expand / Collapse