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.
Author |
Topic |
QAZAFI
Yak Posting Veteran
50 Posts |
Posted - 2009-07-29 : 18:06:24
|
Hi Thereis there any way I can take the average of next three records avery time in the table. for example i have a tableCREATE TABLE #MyTable( ID int NOT NULL,Number int NOT NULL )INSERT #MyTable(ID, Number)SELECT 1, 1 UNION ALLSELECT 2, 3 UNION ALLSELECT 3, 2 UNION ALLSELECT 4, 4 UNION ALLSELECT 5, 6 UNION ALLSELECT 6, 8 UNION ALLSELECT 7, 10I am expecting resultasID Avearge1 22 33 4 and so on is any body have any idea how can I do this with out using cursorsMany thanksQazafi |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-07-29 : 18:11:31
|
[code]DECLARE @Sample TABLE ( ID INT PRIMARY KEY CLUSTERED, Number INT NOT NULL )INSERT @SampleSELECT 1, 1 UNION ALLSELECT 2, 3 UNION ALLSELECT 3, 2 UNION ALLSELECT 4, 4 UNION ALLSELECT 5, 6 UNION ALLSELECT 6, 8 UNION ALLSELECT 7, 10SELECT s.ID, f.aFROM @Sample AS sCROSS APPLY ( SELECT AVG(1.0 * Number) FROM @Sample AS x WHERE x.ID BETWEEN s.ID AND s.ID + 2 ) AS f(a)[/code] N 56°04'39.26"E 12°55'05.63" |
 |
|
asgast
Posting Yak Master
149 Posts |
Posted - 2009-07-30 : 04:12:53
|
The best use of cross apply I have ever seen :) |
 |
|
|
|
|
|
|