LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 8
Default Speed-up Script

Hello, I have two tables, two user-defined functions, and one script.
all of these objects are detailed below, including CREATE TABLE
statements, with CREATE INDEX lines.

the sript is working properly, but going very slowly. I am hoping that
someone can help me to make it more efficient! Thank you.

SCRIPT:
DECLARE cc CURSOR
FOR SELECT dteDate
FROM tblEndDates
ORDER BY dteDate

DECLARE @now datetime
DECLARE @then datetime
DECLARE @months int
DECLARE @year int, @month int

OPEN cc
FETCH NEXT FROM cc into @now
WHILE @@FETCH_STATUS=0

BEGIN

SET @months = 10
WHILE(@months<32)

BEGIN

SET @year = year(@now)
SET @month = month(@now)
SET @then = CONVERT(smalldatetime, str(@month) + '/1/' + str(@year))

INSERT INTO tblIR_LB ( dteDate,
[sglSTDEV(upper)],
[sglSTDEV(lower)],
intMA,
dblPL,
dblSTDEV,
dblIR,
intTotalDays,
intExposure,
intLB )
SELECT TOP 1 @now,
T1.[sglSTDEV(upper)],
T1.[sglSTDEV(lower)],
T1.[intMA],
dbo.GetPL([PL], [TOTALDAYS]) AS PL_Result,
dbo.GetSTDEV(T1.[SUM], T1.[SUMSQ], T1.[TOTALDAYS]) AS [STDEV],
dbo.GetPL([PL], [TOTALDAYS])/dbo.GetSTDEV(T1.[SUM], T1.[SUMSQ],
T1.[TOTALDAYS]) AS IR,
T1.TOTALDAYS,
T1.EXPOSURE,
@months+1
FROM (SELECT SUM([dblPL]*[intTotalDays]) AS PL,
SUM([dblSUMSQ]) AS SUMSQ,
SUM([dblSUM]) AS [SUM],
SUM([intExposure]) AS EXPOSURE,
SUM([intTotalDays]) AS TOTALDAYS,
[sglSTDEV(upper)],
[sglSTDEV(lower)],
[intMA]
FROM data
WHERE dteDate BETWEEN DATEADD(m, -@months, @then) AND @now
GROUP BY [sglSTDEV(upper)], [sglSTDEV(lower)],[intMA]) AS T1
ORDER BY IR DESC, [sglSTDEV(upper)] DESC, [sglSTDEV(lower)]
DESC,[intMA] DESC

SET @months = @months + 1

END

FETCH NEXT FROM cc into @now

END

CLOSE cc

DEALLOCATE cc



FUNCTIONS:

CREATE FUNCTION GetSTDEV(@dblSUM float, @dblSUMSQ float, @days int)
RETURNS float
AS
BEGIN
DECLARE @result float
SET @result = SQRT(((@days * @dblSUMSQ) - POWER(@dblSUM, 2)) /
(@days * (@days - 1))) * SQRT(252)
IF @result = 0
BEGIN
RETURN 1
END

RETURN @result
END



CREATE FUNCTION GetPL(@dblPL float, @days int)
RETURNS float
AS
BEGIN
IF @days < 0
BEGIN
RETURN (@dblPL / @days) * 252
END
RETURN 0
END




TABLES:

CREATE TABLE [dbo].[data] (
[dteDate] [smalldatetime] NULL ,
[dblPL] [float] NULL ,
[dblSUMSQ] [float] NULL ,
[dblSUM] [float] NULL ,
[intExposure] [float] NULL ,
[dblPL(mean)] [float] NULL ,
[dblSUMSQ(mean)] [float] NULL ,
[dblSUM(mean)] [float] NULL ,
[intExposure(mean)] [float] NULL ,
[intTotalDays] [int] NULL ,
[sglSTDEV(upper)] [float] NULL ,
[sglSTDEV(lower)] [float] NULL ,
[intMA] [smallint] NULL
) ON [PRIMARY]
GO

CREATE CLUSTERED INDEX [IDX_dteDate] ON [dbo].[data]([dteDate]) ON
[PRIMARY]
GO



CREATE TABLE [dbo].[tblEndDates] (
[dteDate] [smalldatetime] NULL
) ON [PRIMARY]
GO

CREATE CLUSTERED INDEX [IDX_dteDate] ON
[dbo].[tblEndDates]([dteDate]) ON [PRIMARY]
GO

 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can you speed UP drag speed? Ryan W Excel Discussion (Misc queries) 1 October 24th 05 06:09 PM
I need mor Speed!!!! MESTRELLA29 Excel Discussion (Misc queries) 0 February 11th 05 02:51 PM
Speed tjh Excel Programming 1 December 16th 04 05:49 PM
need for speed! Neil[_19_] Excel Programming 1 May 19th 04 10:25 PM
Excel 2000/XP script to Excel97 script hat Excel Programming 3 March 2nd 04 03:56 PM


All times are GMT +1. The time now is 05:50 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"