Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Development Tools
 ASP.NET
 Running total for over 70000000 recordes

Author  Topic 

fan2005
Yak Posting Veteran

85 Posts

Posted - 2011-01-25 : 09:51:40
hi
I have a table which has all bank accounts and its transactions per account and date of transaction

the result should be sum of the remain amount for each account in each date

i used this solution but for over 70000000 records it seems it never ends

I'm looking for the fastest way may be it needs clr

,the sql server i should use is sql server2000
thanks in advanced




create database center8
go

USE [center8]
GO

/****** Object: Table [dbo].[Allacounts] Script Date: 01/11/2011 15:27:33 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[Allacounts](
[row] [int] IDENTITY(1,1) NOT NULL,
[account] [nchar](10) NULL
) ON [PRIMARY]

GO



/****** Object: Table [dbo].[Gardesh] Script Date: 01/11/2011 15:27:45 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[Gardesh](
[id] [int] IDENTITY(1,1) NOT NULL,
[amount] [decimal](18, 2) NULL,
[tarikh] [nchar](10) NULL,
[account] [nchar](10) NULL,
CONSTRAINT [PK_Gardesh] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO


INSERT INTO [dbo].[Allacounts]
([account])
VALUES
('11'),
('12'),
('13')



INSERT INTO [dbo].[Gardesh]
([tarikh]
,[amount]
,[account])
VALUES
('2010/01/02' ,50000 , '11' ),
('2010/05/05' ,-3000 , '11' ),
('2010/06/06' ,1000 , '11' ),
('2010/01/03' ,65000 , '12' ),
('2010/05/01' ,6000 , '12' ),
('2010/01/01' ,100000, '13' ),
('2010/09/07' ,-1000 , '13' )
GO

---------------------------------------------
CREATE TABLE tblmandeh (tarikh nchar(12),account nvarchar(10), gardesh decimal(18,2), mandeh decimal(18,2))

delete from tblmandeh
DECLARE @tarikh nchar(12),
@account nvarchar(10),
@gardesh decimal(18,2),
@mandeh decimal(18,2)




declare @id int set @id = 1

while @id<=(select COUNT(account) from dbo.Allacounts)
begin

IF OBJECT_ID('tempdb..#mytemptable') IS NOT NULL
DROP TABLE #mytemptable;

select a.tarikh,a.account,a.amount as gardesh into #mytemptable from dbo.Allacounts b left outer join dbo.Gardesh a
on b.account=a.account
where @id=row
order by tarikh,account

SET @mandeh = 0

DECLARE rt_cursor CURSOR
FOR
SELECT tarikh,account, gardesh
FROM #mytemptable

OPEN rt_cursor

FETCH NEXT FROM rt_cursor INTO @tarikh,@account,@gardesh

WHILE @@FETCH_STATUS = 0
BEGIN
SET @mandeh = @mandeh + @gardesh
INSERT dbo.tblmandeh VALUES (@tarikh,@account,@gardesh,@mandeh)
FETCH NEXT FROM rt_cursor INTO @tarikh,@account,@gardesh
END

CLOSE rt_cursor
DEALLOCATE rt_cursor



set @id=@id+1
end
----------out put----------------------
select * from tblmandeh
tarikh account gardesh mandeh
2010/01/02 11 50000.00 50000.00
2010/05/05 11 -3000.00 47000.00
2010/06/06 11 1000.00 48000.00
2010/01/03 12 65000.00 65000.00
2010/05/01 12 6000.00 71000.00
2010/01/01 13 100000.00 100000.00
2010/09/07 13 -1000.00 99000.00

dataguru1971
Master Smack Fu Yak Hacker

1464 Posts

Posted - 2011-01-25 : 09:58:57
See this article before Jeff Moden's head explodes. you can do this without a cursor.

http://www.sqlservercentral.com/articles/T-SQL/68467/



Poor planning on your part does not constitute an emergency on my part.
Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2011-01-25 : 10:08:46
quote:
See this article before Jeff Moden's head explodes.
Mine's already gone thinking about someone actually reading a 70 million row report that needs a running total.
Go to Top of Page

3magic
Starting Member

14 Posts

Posted - 2011-01-31 : 08:29:31
Hi,thanks for sharing the information here.Really a good information is presented here and very useful for me also because i'm beginner in programming area.Thanks a lot again for sharing the information here

unspammed
Go to Top of Page

Lumbago
Norsk Yak Master

3271 Posts

Posted - 2011-01-31 : 09:23:46
quote:
See this article before Jeff Moden's head explodes. you can do this without a cursor.
http://www.sqlservercentral.com/articles/T-SQL/68467/
VERY interesting publication date for that article :)

- Lumbago

My blog-> www.thefirstsql.com
Go to Top of Page

dataguru1971
Master Smack Fu Yak Hacker

1464 Posts

Posted - 2011-01-31 : 12:45:53
quote:
Originally posted by Lumbago

quote:
See this article before Jeff Moden's head explodes. you can do this without a cursor.
http://www.sqlservercentral.com/articles/T-SQL/68467/
VERY interesting publication date for that article :)

- Lumbago

My blog-> www.thefirstsql.com



He is using DENALI's new DBCC OPENWORMHOLE function to write articles from the future.



Poor planning on your part does not constitute an emergency on my part.
Go to Top of Page

Lumbago
Norsk Yak Master

3271 Posts

Posted - 2011-02-01 : 02:58:46
Oh...cool! I was thinking it had something to do with PREDICTUTCDATE() or something. Gotta get my hands on that Denali release

- Lumbago

My blog-> www.thefirstsql.com
Go to Top of Page

dataguru1971
Master Smack Fu Yak Hacker

1464 Posts

Posted - 2011-02-01 : 05:45:34
Yeah no doubt! they added that feature for people who have no backups and need to restore deleted or lost data.



Poor planning on your part does not constitute an emergency on my part.
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2011-02-01 : 06:59:54
Even if you read 10 rows per second in your running total, it will take 81 days to read the full report of 350,000 pages (100 lines per page, 2 pages per sheet).
The report will take 12 hours to print and weigh 1,500 kg (the weight of a larger car).
A tree can be converted into about 80,000 sheets of paper so the running total will need about 4 trees.

Great idea!



N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page

Lumbago
Norsk Yak Master

3271 Posts

Posted - 2011-02-01 : 07:27:34
Whats all this talk about a report? The OP isn't (as far as I can see) saying anything about this being a report...?

- Lumbago
My blog-> www.thefirstsql.com
Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2011-02-01 : 07:41:16
In this thread: http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=155896
Go to Top of Page
   

- Advertisement -