If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Help with Group By, Order By, LIMIT

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 11-25-08, 14:54
pgb205 pgb205 is offline
Registered User
 
Join Date: Nov 2008
Posts: 2
Help with Group By, Order By, LIMIT

I want to retrieve data from a single table and then group it by column1, then subgroup it by column2 , then sort it within each one of those groups and finally display upto 10 results from each one of those sorted groups. Can someone please help with pseudocode as I'm having trouble making this work. Right now it looks like
Select * from table_name where some_condition=TRUE group by column1, column2 order by column3. I'm not even sure how to work the limit clause into this statement as this causes only 10 values to be retrieved overall not 10 from every group.
Reply With Quote
  #2 (permalink)  
Old 01-04-09, 10:01
galih galih is offline
Registered User
 
Join Date: Feb 2008
Location: Bandung - Indonesia
Posts: 15
Select * from table_name
where some_condition=TRUE
group by column1, column2
order by column1, column2 ASC LIMIT 10

ASC - Ascending
DESC - Descending , you choose.
__________________
Forum Informatika - Indonesian Informatics Online Community - http://if.web.id
Reply With Quote
  #3 (permalink)  
Old 01-04-09, 16:19
guelphdad guelphdad is offline
Registered User
 
Join Date: Mar 2004
Posts: 440
when you say group the data do you mean use a group by clause to compress like data (i.e. count how many cars you have irrespective of make/model?)

it sounds to me that you are misusing group in a database context and by group you mean put all cars in order and then trucks and within cars all yellow cars and then red.

If I'm misunderstanding perhaps some sample data and your expected output would clarify.
Reply With Quote
  #4 (permalink)  
Old 01-04-09, 16:41
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
Code:
SELECT column1
     , column2
     , column3
  FROM daTable AS T
 WHERE some_condition=TRUE
   AND ( SELECT COUNT(*) 
           FROM daTable  
          WHERE some_condition=TRUE
            AND column1 = T.column1
            AND column2 = T.column2
            AND column3 > T.column3 ) < 10
GROUP
    BY column1
     , column2
ORDER
    BY column1
     , column2
     , column3 DESC
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On