Click here to Skip to main content
11,632,778 members (82,590 online)
   

Database

 
GeneralRe: How do you use the AVG api in sql to manage grouped output? Pin
Xarzu11-Aug-12 13:30
memberXarzu11-Aug-12 13:30 
AnswerRe: How do you use the AVG api in sql to manage grouped output? Pin
Paul Conrad11-Aug-12 13:32
memberPaul Conrad11-Aug-12 13:32 
AnswerRe: How do you use the AVG api in sql to manage grouped output? Pin
Eddy Vluggen10-Aug-12 22:17
memberEddy Vluggen10-Aug-12 22:17 
AnswerRe: How do you use the AVG api in sql to manage grouped output? Pin
Niladri_Biswas21-Aug-12 22:51
mvpNiladri_Biswas21-Aug-12 22:51 
All Version of Sql server

Select City, AVG(Salary) AS Salary
FROM @t
Group By City
Order By City DESC

This can also be done by treating AVG function as Analytical function which is available since SQL Server 2005

Select City,Salary From
(
    Select
        Rn = Row_Number() Over(PARTITION BY City Order By (Select 1))
        ,City
        ,AVG(Salary) OVER(PARTITION BY City) AS Salary
    From @t) X
Where X.Rn = 1
Order By City DESC

The answer for both the cases

City Salary
Seattle 20000
Redmond 30000
Niladri Biswas
(Code Project MVP 2012)

QuestionHow to store a standard selection? Pin
Jörgen Andersson10-Aug-12 9:57
memberJörgen Andersson10-Aug-12 9:57 
AnswerRe: How to store a standard selection? Pin
mark merrens10-Aug-12 10:02
membermark merrens10-Aug-12 10:02 
GeneralRe: How to store a standard selection? Pin
Jörgen Andersson10-Aug-12 10:09
memberJörgen Andersson10-Aug-12 10:09 
GeneralRe: How to store a standard selection? Pin
mark merrens10-Aug-12 10:41
membermark merrens10-Aug-12 10:41 
AnswerRe: How to store a standard selection? Pin
Eddy Vluggen10-Aug-12 10:11
memberEddy Vluggen10-Aug-12 10:11 
GeneralRe: How to store a standard selection? Pin
Jörgen Andersson10-Aug-12 10:31
memberJörgen Andersson10-Aug-12 10:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.


Advertise | Privacy | Mobile
Web02 | 2.8.150728.1 | Last Updated 27 Jul 2015
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid