SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast
    Join Date
    Oct 2009
    Posts
    63
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Group By using Order By

    Im trying to crack a query to list my details in particular order. My Tables are as mentioned below.

    Car
    Code:
    company_id |  car_company
    1  	               Honda
    2  	               Toyota
    3 	               Bmw
    4                     Benz
    Model
    Code:
    model_id | company_id  |	model_name
    1  	          1               Jazz
    2  	          2               Corola
    3  	          1               City
    4  	          1               Civic
    5 	          2               Hybrid
    6  	          1   		   Accord
    7 	          2               Camry
    8 	          3               Roadster
    9 	          4		   S-Class
    10	          3		   M3 Coupe
    11	          3		   M5 Sedan
    12	          4		   E-Class
    13	          4 		   SLK
    14	          4		   SLR
    Sales
    Code:
    id |  mode_id   
    1  	1
    2  	2
    3  	1
    4  	1
    5  	2
    6  	1
    7  	2
    8  	2
    9  	2
    10 	3
    11 	3
    12 	3
    13 	4
    14 	3
    15 	4
    16 	3
    17     4
    18     4
    19     10
    20     11
    21     12
    22	13
    23     5
    16     6
    24     8
    25     9
    26     10
    27     11
    28     12
    29	13
    For example if i take Sales list of Honda Car(company_id =1)

    Model with maximum sales should be on top,
    If sales counts are equal the it should be in the Alphabetic Order.

    Results
    Code:
    car_company | model_name | sales_count | model_id
    
    Honda 	  	City      	      5   	3
    Honda 	  	Civic     	      3   	4
    Honda 	  	Jazz      	      3   	1
    Honda 	  	Accord    	      0   	6

    Thanks in advance

  2. #2
    SitePoint Wizard guelphdad's Avatar
    Join Date
    Oct 2003
    Location
    St. Catharines, ON Canada
    Posts
    1,708
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    ORDER BY 
    sales_count DESC,
    model_name


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
  •