September 29, 2005 at 9:20 am
Hi
Here is my problem.
I have a table
Stockid Time Amt
1002 11:23 25.10
1002 11:20 25.62
1002 10:40 24.80
1002 10:39 25.25
1002 10:38 23
1003 11:22 79.6
1003 11:38 78
1003 11:40 80
I need an average of the delta's for every stock
i.e.
for 103 I should get the average of 80-78 = 2 & 79.6-78 = 1.6
so average of 2 & 1.6
Thanks
Newbie
September 29, 2005 at 10:40 am
I cannot think of a way to do that without a Cursor
September 29, 2005 at 10:43 am
The following is the pseudo code:
DECLARE @TmpTable (RID int identity, stockid, time, Amt, Delta)
INSERT @TmpTable (Stockid,time,Amt, Delta)
SELECT Stockid,time,Amt,0
FROM YourTable
ORDER BY Stockid, time
UPDATE A SET Delta=b.Amt- a.Amt -- If you need abs change, add ABS()
FROM @TmpTable a, @TmpTable b
WHERE a.Stockid=b.Stockid and a.rid=b.rid-1
SELECT StockID, CAST(SUM(Delta)/(COUNT(*)-1) as numeric(10.2)) AS AvgAmt
FROM @TmpTable
GROUP BY StockID
HAVING COUNT(*)>1
September 29, 2005 at 10:47 am
I guess you could do it without a Cursor
Did not see the Time part
September 30, 2005 at 9:24 am
I tried this but mysql does'nt allow and update from statement,so this does'nt work
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply