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 > SQL Query help...

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-26-09, 17:30
tjones1105 tjones1105 is offline
Registered User
 
Join Date: Dec 2007
Posts: 21
SQL Query help...

Hello,
I'm trying to figure out how to Limit my results to the top 3 records returned. Now thats the simple part and I have it working just fine. But now I want to add one more result with a count of all remaining records.

For example

Code:
Select user, Count(user) AS Totals
From Sales
Group By user
Order By user DESC
Limit 0,3
Would give me
joe 25
steve 17
amy 12

But What I want is and cant figure out :-)
joe 25
steve 17
amy 12
Everyone Else 25

I'm using MySQL 5.0.45

Thanks,
tom
Reply With Quote
  #2 (permalink)  
Old 08-26-09, 20:12
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
here's a easy way to do it, but which requires a small amount of math in the application language

Code:
SELECT 2 AS row_type
     , user
     , COUNT(*) AS Totals
  FROM Sales
GROUP 
    BY user
UNION ALL
SELECT 1 AS row_type
     , NULL
     , COUNT(*) AS Totals
  FROM Sales
ORDER 
    BY row_type
     , user DESC
LIMIT 4
this query returns the totals for everybody as the first row, then the totals for the top 3 users as the 2nd, 3rd, and 4th row

make sense?

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 08-27-09, 18:57
tjones1105 tjones1105 is offline
Registered User
 
Join Date: Dec 2007
Posts: 21
Thanks,
it kind of works. The only problem is that "Everyone Else"/null result is the total of all of the records not the remaining.

Thanks,
tom
Reply With Quote
  #4 (permalink)  
Old 08-27-09, 19:10
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
Quote:
Originally Posted by tjones1105
The only problem is that "Everyone Else"/null result is the total of all of the records not the remaining.
that's not a problem -- that's the way it's designed

okay, your application logic will loop over the 4 rows in the result set, right?

okay, to process the first row, store the number but don't print anything

for each of the next three rows, print the number for that user, and subtract that number from the stored "everyone else" total

after you have done the 4th row, i.e. the 3rd user, print what's left in "everyone else"

neat, eh

__________________
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