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
 General SQL Server Forums
 New to SQL Server Programming
 Average with Condition

Author  Topic 

hiklior
Starting Member

2 Posts

Posted - 2008-05-28 : 17:03:30
Problem

fldyear fldascending flddecending
2007 20 23
2006 21 14
2007 41 12
2007 12 5


how do i find the average of fld ascending and descending where year=2007

Your help will be appreciated

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-05-28 : 17:14:28
Use the AVG function.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Database maintenance routines:
http://weblogs.sqlteam.com/tarad/archive/2004/07/02/1705.aspx
Go to Top of Page

hiklior
Starting Member

2 Posts

Posted - 2008-05-28 : 17:14:34
i also want it to be grouped by year ( which has to be distinct) . i appreciate your help
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-05-28 : 17:27:09
Please show us what you have tried.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Database maintenance routines:
http://weblogs.sqlteam.com/tarad/archive/2004/07/02/1705.aspx
Go to Top of Page

LTack
Posting Yak Master

193 Posts

Posted - 2008-05-28 : 17:29:48
SELECT avg(fldascending), avg(flddescending) FROM _table_ WHERE year=2007 GROUP BY year;

^^ I'm pretty new to SQL, so not sure if I'm right on this or not.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-05-28 : 17:31:41
That looks fine. Is it returning an error or not returning the correct results?

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Database maintenance routines:
http://weblogs.sqlteam.com/tarad/archive/2004/07/02/1705.aspx
Go to Top of Page

LTack
Posting Yak Master

193 Posts

Posted - 2008-05-28 : 17:39:16
*twiddles thumbs*

Why do I stalk forums, why do I stalk forums, why do I--hi.

(Thanks, tkizer...I was nervous attempting to post a solution, lol)
Go to Top of Page
   

- Advertisement -