yovys
04-14-2004, 06:25 AM
[|)] Help me please!!!
I need to calculate a field in reporting services, but this field is a cumulate value, and it depends the value of the previous row, is this: I have two columns, the second sum the value of the first column plus the value of its own column in the previous row
Eternally I will be been thankful with any suggestion
Yovys
KPayne
04-16-2004, 01:17 AM
Since I don't know the specifics of your table, I've included a T-SQL (SQL Server 2000) example based on a ficticious table.
[quote]
SETQUOTED_IDENTIFIERON
GO
SETANSI_NULLSON
GO
CREATEPROCEDUREGetCumulativeTotal(@StartDatedateti me,@EndDatedatetime)AS
SETNOCOUNTON
CREATETABLE#RunningTotalCache(MyPKint,Pricemoney)
DECLARERunningTotalCursorCURSORFAST_FORWARDFOR
SELECTMyPK,Price
FROMMyTable
WHEREMyDateBETWEEN@StartDateAND@EndDate
ORDERBYMyDate
DECLARE@RunningTotalPricemoney
DECLARE@CurrentPricemoney
DECLARE@CurrentPKint
SET@RunningTotalPrice=0
BEGINTRANSACTION
OPENRunningTotalCursor
FETCHNEXTFROMRunningTotalCursorINTO@CurrentPK,@Cur rentPrice
WHILE@@FETCH_STATUS=0
BEGIN
INSERTINTO#RunningTotalCache(MyPK,Price)VALUES(@Cu rrentPK,@RunningTotalPrice+@CurrentPrice)
SET@RunningTotalPrice=@RunningTotalPrice+@CurrentP rice
FETCHNEXTFROMRunningTotalCursorINTO@CurrentPK,@Cur rentPrice
END
CLOSERunningTotalCursor
DEALLOCATERunningTotalCursor
SELECTMyTable.*,tot.Price
FROMMyTable
INNERJOIN#RunningTotalCachetot
ONtot.MyPK=MyTable.MyPK
WHEREMyTable.MyDateBETWEEN@StartDateAND@EndDate
COMMITTRANSACTION
GO
SETQUOTED_IDENTIFIEROFF
GO
SETANSI_NULLSON
GO
</CODE>
To execute this stored procedure from Query Analyzer:
[quote]
execgetcumulativetotal'01/01/01','01/01/05'
</CODE>
To create the table that this example uses:
[quote]
CREATETABLE[MyTable](
[MyPK][int]IDENTITY(1,1)NOTNULL,
[MyDate][datetime]NULLCONSTRAINT[DF__MyTable__MyDate__1EA48E88]DEFAULT(getdate()),
[Price][money]NULL
)ON[PRIMARY]
GO
/*Loadthetablewithtestdata:
Executethislinemultipletimestofillthetable*/
INSERTINTO[MyTable]([Price])VALUES(RAND()*1000)
</CODE>
Try this out to see how it works and post again if you still have any questions.
Good luck!
Keith Payne
Technical Marketing Solutions
vBulletin® ©Jelsoft Enterprises Ltd.