Go Back  HTML Forums - Free Webmaster Forums and Help Forums > WEBSITE DEVELOPMENT > Databasing
User Name:
Password:
 

Reply
Thread Tools   Display Modes
  View First Unread
 
Old 08-02-2009, 01:12 PM
  #1
cmetz1977
Champion (Level 13)
 
cmetz1977's Avatar
 
Join Date: May 2008
Location: Lakeland, FL
Posts: 217
iTrader: (0)
cmetz1977 will become famous soon enough
Sorting by a pre-sub-grouped number of records

I have a MySQL query and I would like to sort by the total results of first item in the GROUP statement.

For example:
PHP Code:
SELECT `name`, `address`, count( `address` ) as 'count' FROM `tableGROUP BY `name`, `address
Let's say there are 20 records for "John Smith", all with a unique address, and only 3 records for "Bob Miller", all with the same address. If I used ORDER BY `count` DESC at the end of my statement, "Bob Smith" would be listed first because "count" would be 3 where all the "count"s for "John Smith" would be 1.

Is there a statement that would allow me to rank "John Smith" higher in the sort since he has more overall records, while still letting me sub-group by address?
__________________
Never accept Kool-aid from someone who won't drink from the same punch bowl.
cmetz1977 is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 08-02-2009, 06:40 PM
  #2
Horus_Kol
Mod of the Underlay
 
Horus_Kol's Avatar
 
Join Date: Jun 2002
Location: At a desk, hooked up and ready to rock
Posts: 17,355
iTrader: (0)
Horus_Kol is a glorious beacon of lightHorus_Kol is a glorious beacon of lightHorus_Kol is a glorious beacon of lightHorus_Kol is a glorious beacon of lightHorus_Kol is a glorious beacon of light
You can order by aggregate values:

PHP Code:
SELECT `name`, `address`, count( `address` ) as `countFROM `tableGROUP BY `name`, `addressORDER BY `countDESC
__________________
New Photo Gallery: Modus InOperandi
Personal Blog: HorusKol
Articles on Programming and Development (PHP/HTML/CSS, C/C++, more): RandomTweak
Horus_Kol is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 08-03-2009, 11:39 AM
  #3
cmetz1977
Champion (Level 13)
 
cmetz1977's Avatar
 
Join Date: May 2008
Location: Lakeland, FL
Posts: 217
iTrader: (0)
cmetz1977 will become famous soon enough
I'm getting a headache trying to think of this from all the angles. I think I am going to run
PHP Code:
SELECT `address`, count(`address` ) as 'count' FROM `tableGROUP BY `addressORDER BY `countDESC 
then use that as a map for all the possible names at each address result.

Our clients will sometimes put in 'need address' or 'n/a', ... for their customer's addresses. We are trying to distinguish when multiple customers have the same address in situations like these, from companies that have multiple entries with the same name and same address. My problem doesn't just lie within this query but trying to design the entire interface and logic.
__________________
Never accept Kool-aid from someone who won't drink from the same punch bowl.
cmetz1977 is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote

Reply
KEEP TABS
SPONSORS
 
Boxedart



 
 


 
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
  
 
 
 



 
  POSTING RULES
 
 
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Thread Tools
Display Modes

Forum Jump

 

All times are GMT -5. The time now is 10:42 AM.

   

Mascot team created by Drawshop.com

Powered by vBulletin® Version 3.6.7
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.

Server Monitoring by ENIACmonitor 0.01
HTMLforums.com © Big Resources, Inc. Web Design by BoxedArt.com
vRewrite 1.5 beta SEOed URLs completed by Tech Help Forum and Chalo Na.