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 2005 Forums
 Transact-SQL (2005)
 Avg every set of records

Author  Topic 

Nowy
Yak Posting Veteran

57 Posts

Posted - 2007-06-13 : 08:48:57
First:
- I've an table "Category" with 2 columns, "CatID", "Cat", "Package"
- I've an table "Product" with 2 columns, "ProID", "CatID", "price"
- About several reasons, it isn't possible to change the db structure. The table names and column names are actual to keep it simple.

Table "Category":
Sometimes there is 1 record with an "Package" name, but sometimes there are 3 records with the same "Package" name, etc.

I want for every record with the same "Package" name the average "price" from table "Product" and put it in an new table "Test"

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2007-06-13 : 08:58:32
[code]Insert into Test (co1l, col2)
Select c.Package, AVG(p.Price) as AvgPrice
From Category c Join Product p on c.CatID = p.CatID
group by c.Package[/code]

Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2007-06-13 : 09:00:52
[code]select Package, avg(price)
from Category c inner join Product p
on c.CatID = p.CatID
group by Package[/code]


KH

Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2007-06-13 : 09:02:42



KH

Go to Top of Page
   

- Advertisement -