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
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Avg Question

Author  Topic 

Mazdak
Yak Posting Veteran

63 Posts

Posted - 2005-11-27 : 04:24:15
For example I have 500 row , I want to get avg of each 5 field and put it in other table(only for the first 100 row) , it means I will have 20 new row to put in another table , how can I use Avg function in this case?

Thanks a lot

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2005-11-27 : 07:44:00
By creating a zero-based identity column you can group by the identityColumn / 5:

--create a table with an identity column
create table Junk (rowid int identity(0,1), objectid int)
go
--insert 500 rows
insert junk (objectid)
select distinct top 500 a.id from sysobjects a cross join sysobjects b
order by a.id
go

--avg every 5 row values of 1st 100 rows
select convert(varchar,min(rowid)+1)+'-'+convert(varchar,max(rowid)+1) [group]
,count(*) [numberAveraged]
,avg(objectid) [average]
from junk
where rowid <100
group by rowid/5

go
drop table junk


Be One with the Optimizer
TG
Go to Top of Page
   

- Advertisement -