Thread: Grouping By

    #1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2000
    Posts
    22
    Rep Power
    0
    I'm doing a group by statement in MySQL, heres some example data.

    Name Age
    White 20
    Black 17
    White 22

    SELECT * FROM [tblname] GROUP BY Name.

    When I do this group by statement, the statement returns the average of the ages rather than the sum of which is need. Do I need to do a loop to get each name and add the total, or is there another way of doing it...by using the SUM statement for example.
  2. #2
  3. No Profile Picture
    christucker
    Guest
    Devshed Newbie (0 - 499 posts)
    Your SQL should be:

    SELECT name, SUM(age)
    FROM <table_name>
    GROUP BY name;

    MySQL just chose AVE for you as the default aggregate function to apply to the non-grouped column -- if you want it to do something different, you have to explicitly tell it so.

Similar Threads

  1. Grouping items
    By markru in forum PHP Development
    Replies: 7
    Last Post: December 18th, 2003, 11:23 AM
  2. Grouping data by month from datetime feild
    By mikeShank in forum MySQL Help
    Replies: 2
    Last Post: September 16th, 2003, 07:30 PM
  3. php tables- Grouping data
    By mrbvns1 in forum PHP Development
    Replies: 2
    Last Post: July 15th, 2003, 03:27 PM
  4. problem with aggregate function & grouping
    By Moichi in forum MySQL Help
    Replies: 5
    Last Post: January 22nd, 2002, 01:10 PM
  5. complicated select (grouping)
    By Peter.vullings in forum MySQL Help
    Replies: 1
    Last Post: August 21st, 2001, 05:27 PM

IMN logo majestic logo threadwatch logo seochat tools logo