Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. How do you use the AVG api in sql to manage grouped output?

How do you use the AVG api in sql to manage grouped output?

Scheduled Pinned Locked Moved Database
databasejsontutorialquestioncareer
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • X Offline
    X Offline
    Xarzu
    wrote on last edited by
    #1

    How do you use the AVG api in sql to manage grouped output? For example: Given a SQL Table “Employee” Name Salary City John Doe 15000 Seattle Jane Doe 30000 Redmond Tim Wayne 25000 Seattle Write a SQL query that would return the average salary per city in the format shown below: City Salary Seattle 20000 Redmond 30000

    P L N 3 Replies Last reply
    0
    • X Xarzu

      How do you use the AVG api in sql to manage grouped output? For example: Given a SQL Table “Employee” Name Salary City John Doe 15000 Seattle Jane Doe 30000 Redmond Tim Wayne 25000 Seattle Write a SQL query that would return the average salary per city in the format shown below: City Salary Seattle 20000 Redmond 30000

      P Offline
      P Offline
      Paul Conrad
      wrote on last edited by
      #2

      Xarzu wrote:

      Write a SQL query that would return the average salary per city in the format shown below:

      For some reason this sounds like homework. What have you done so far for a query?

      "Any sort of work in VB6 is bound to provide several WTF moments." - Christian Graus

      X 1 Reply Last reply
      0
      • X Xarzu

        How do you use the AVG api in sql to manage grouped output? For example: Given a SQL Table “Employee” Name Salary City John Doe 15000 Seattle Jane Doe 30000 Redmond Tim Wayne 25000 Seattle Write a SQL query that would return the average salary per city in the format shown below: City Salary Seattle 20000 Redmond 30000

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        Xarzu wrote:

        How do you use the AVG api function in sql

        It's not an "API".

        Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]

        1 Reply Last reply
        0
        • P Paul Conrad

          Xarzu wrote:

          Write a SQL query that would return the average salary per city in the format shown below:

          For some reason this sounds like homework. What have you done so far for a query?

          "Any sort of work in VB6 is bound to provide several WTF moments." - Christian Graus

          X Offline
          X Offline
          Xarzu
          wrote on last edited by
          #4

          Select City, AVG(Salary) from Employee where ... I don't know the rest Where city == city or something like that?

          P 1 Reply Last reply
          0
          • X Xarzu

            Select City, AVG(Salary) from Employee where ... I don't know the rest Where city == city or something like that?

            P Offline
            P Offline
            Paul Conrad
            wrote on last edited by
            #5

            You could try:

            select city, avg(salary) from Employee group by city

            This will give you the average grouped by cities. How to sort the cities in the order you want to order by in your expected result, I'm leaving that as an exercise :-D

            "Any sort of work in VB6 is bound to provide several WTF moments." - Christian Graus

            1 Reply Last reply
            0
            • X Xarzu

              How do you use the AVG api in sql to manage grouped output? For example: Given a SQL Table “Employee” Name Salary City John Doe 15000 Seattle Jane Doe 30000 Redmond Tim Wayne 25000 Seattle Write a SQL query that would return the average salary per city in the format shown below: City Salary Seattle 20000 Redmond 30000

              N Offline
              N Offline
              Niladri_Biswas
              wrote on last edited by
              #6

              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)

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups