Getting AVERAGE field value in query

Last post 11-08-2009 12:18 AM by Naom. 5 replies.

Sort Posts:

  • Getting AVERAGE field value in query

    11-06-2009, 11:13 PM
    • Participant
      826 point Participant
    • zoltac007
    • Member since 04-06-2006, 4:25 PM
    • Posts 1,560

    How would I write a query that would select a group of records and give me the average value of one of the fields?

    SELECT ID, Name, Amount
    FROM tblCustomers
    WHERE Amount > 10

    Assuming the query found 100 qualifying records what would the statements look like that would give me the average value of the 'Amount' field? 

    Thank you

  • Re: Getting AVERAGE field value in query

    11-06-2009, 11:43 PM
    • Participant
      826 point Participant
    • zoltac007
    • Member since 04-06-2006, 4:25 PM
    • Posts 1,560

    Found the AVG function -

    SELECT AVG(Amount)
    FROM tblNAME

  • Re: Getting AVERAGE field value in query

    11-06-2009, 11:51 PM
    Answer
    • Participant
      1,600 point Participant
    • vipuldonga
    • Member since 02-10-2009, 11:18 AM
    • Ahmedabad
    • Posts 293

    hi,

    i think you want this way or not

    SELECT ID, Name, AVG(Amount) as AvgAmount
    FROM tblCustomers
    WHERE Amount > 10

    Thanks & Regards
    Vipul Donga

    Mark it as an answer, if it helped

    if (MyAnswer)
    MarkAsAnswer();
  • Re: Getting AVERAGE field value in query

    11-07-2009, 8:00 AM
    Answer
    • All-Star
      18,416 point All-Star
    • raghav_khunger
    • Member since 08-18-2008, 8:25 AM
    • Delhi, India
    • Posts 3,426
    • TrustedFriends-MVPs

    Hi,

    Try this


    DECLARE  @tblCustomers TABLE
    (
    ID INT IDENTITY, NAME VARCHAR(20), Amount INT
    )
    
    
    
    INSERT INTO @tblCustomers
    SELECT 'Name1',100 UNION ALL
    SELECT 'Name2',200 UNION ALL
    SELECT 'Name3',300 UNION ALL
    SELECT 'Name4',400 
    
    
    ;WITH CTE AS
    (
    SELECT Amount
    FROM @tblCustomers
    WHERE Amount > 100
    )
    
    SELECT AVG([Amount]) AS [AVERAGEAMOUNT] FROM CTE
    
    --OUTPUT
    -------------
    300
    


    Raghav CodeASP.NET Community | My Blog | jQuery Intellisense in Visual Studio 2008




    "Success doesn't come to you…you go to it."--Marva Collins

    "Failure is success if we learn from it." Malcolm Forbes

    "Success does not come to those who wait . . . and it does not wait for anyone to come to it." Anonymous


  • Re: Getting AVERAGE field value in query

    11-08-2009, 12:16 AM
    • All-Star
      30,475 point All-Star
    • Naom
    • Member since 12-31-2007, 2:08 PM
    • Wisconsin
    • Posts 6,762

    vipuldonga:

    hi,

    i think you want this way or not

    SELECT ID, Name, AVG(Amount) as AvgAmount
    FROM tblCustomers
    WHERE Amount > 10

     

    In order for this query to work you need to add GROUP BY ID,NAME - otherwise you'll get an error in this query.

    Looking for a job opportunity.

    Beware of bugs in the above code; I have only proved it correct, not tried it.
    (Donald Knuth)

    Visit my blog

    PluralSight Learning Library
  • Re: Getting AVERAGE field value in query

    11-08-2009, 12:18 AM
    • All-Star
      30,475 point All-Star
    • Naom
    • Member since 12-31-2007, 2:08 PM
    • Wisconsin
    • Posts 6,762

    If you're using SQL Server 2005 or up, you can add an aggregate result directly into this query, e.g.

     SELECT ID, Name, Amount, AVG(amount) OVER ()
    FROM tblCustomers
    WHERE Amount > 10

    Looking for a job opportunity.

    Beware of bugs in the above code; I have only proved it correct, not tried it.
    (Donald Knuth)

    Visit my blog

    PluralSight Learning Library
Page 1 of 1 (6 items)
Microsoft Communities