SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Wizard
    Join Date
    May 2002
    Posts
    1,370
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    simple averaging of table column

    This is for a single column, and I don't want to group - so not using AVG

    Want to just average all the numerical values in a column, into a single result.

    What is this missing?

    Code:
    <?php 
    $result = "SELECT amounts FROM inventory 
    (SUM( inventory.amounts ) / ( COUNT( mysql_num_rows($result))";
    ?>
    Aside from this, what would be needed to get a median value? Divide mysql_num_rows by 1/2 ?

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,017
    Mentioned
    53 Post(s)
    Tagged
    2 Thread(s)
    Code:
    SELECT SUM(amounts) / COUNT(*)
      FROM inventory
    r937.com | rudy.ca | Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  3. #3
    SitePoint Wizard
    Join Date
    May 2002
    Posts
    1,370
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Rudy.

    You probably already know it but for others here. Below gives

    Resource id #5

    ...Without the fetch.

    Code:
      
    $result = "SELECT SUM(amounts) / COUNT(*) FROM inventory";
    $paid = mysql_query($result);
    $row = mysql_fetch_array($paid);
    $test = $row[0];            // needed for echo
    

  4. #4
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,017
    Mentioned
    53 Post(s)
    Tagged
    2 Thread(s)
    you asked a database question, i gave you a database answer

    if that doesn't work in your php code, then you need to ask a php question in that form

    meanwhile, try assigning a column alias and referencing that (not sure how, because i don't do php)
    Code:
    SELECT AVG(amounts) AS my_average
      FROM inventory
    r937.com | rudy.ca | Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •