SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Jul 2003
    Location
    WA
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Select Statements GROUP BY question

    I am a Mysql newbie and want to write a query that will list top customers by the grand total of their purchases and list the results by grand total. Below is the query I have written, but my GROUP BY is wrong and I am not sure how it whoud be written:

    SELECT sales_slip.customer_id AS 'Customer Number', CONCAT_WS(' ', customer.firstname_customer, customer.lastname_customer) AS 'Customer Name',
    SUM(title.unit_retail) AS 'Total Amount Purchased'
    FROM sales_slip, customer, title
    WHERE sales_slip.customer_id = customer.customer_id AND sales_slip.isbn_id = title.isbn_id
    GROUP BY (SUM(title.unit_retail)) DESC;

    Any help is appreciated.

    bviva

  2. #2
    SitePoint Guru
    Join Date
    Jan 2001
    Location
    Alkmaar, Netherlands
    Posts
    710
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you should group by like
    GROUP BY customer_id

    And you want to user order by like
    ORDER BY 'Total Amount Purchased' DESC

    And also you might make use of limit to show top X customers(X= an integer)
    LIMIT X


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
  •