PostgreSQL Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesPostgreSQL Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
AT&T devCentral & BlackBerry(r) Webcast Series: BlackBerry and GPS -Build Location Awareness into your BlackBerry Applications, July 10th-1:00PM EST. Register Today!
  #1  
Old April 21st, 2008, 12:39 AM
trickma trickma is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2008
Posts: 4 trickma User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 56 m 34 sec
Reputation Power: 0
Simple query help! **PostgreSQL

heya's, I'm new to SQL and trying to create a query which will allow me to count the largest occurance of an instance within a column.

For eg.
Code:

Row 1

car
car
guitar
guitar
guitar
chair

I want to be able to run the query so that it returns the item in Row 1 that has the most occurances, ie:

Row 1
Guitar

I've been trying to use count and a sub query but I'm getting nowhere! Any help will be greatly appreciated

Reply With Quote
  #2  
Old April 21st, 2008, 01:50 AM
shammat shammat is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Oct 2003
Location: Munich, Bavaria
Posts: 963 shammat User rank is Second Lieutenant (5000 - 10000 Reputation Level)shammat User rank is Second Lieutenant (5000 - 10000 Reputation Level)shammat User rank is Second Lieutenant (5000 - 10000 Reputation Level)shammat User rank is Second Lieutenant (5000 - 10000 Reputation Level)shammat User rank is Second Lieutenant (5000 - 10000 Reputation Level)shammat User rank is Second Lieutenant (5000 - 10000 Reputation Level)shammat User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 15 h 15 m 49 sec
Reputation Power: 57
What did you try so far?
Show us your SQL and possible errors that you got, and I'm sure someone will help you (I will)

Reply With Quote
  #3  
Old April 21st, 2008, 02:07 AM
trickma trickma is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2008
Posts: 4 trickma User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 56 m 34 sec
Reputation Power: 0
Something like this:

Code:
SELECT column1 FROM(SELECT column1, count(*) FROM table1 GROUP BY column1) AS a;

this lists all of the items in column 1, what i need now is to be able to add in some sort of "WHERE row has the highest count value"

so that in my original example guitar = 3 and it prints

column1
-----
guitar

Reply With Quote
  #4  
Old April 21st, 2008, 02:45 AM
shammat shammat is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Oct 2003
Location: Munich, Bavaria
Posts: 963 shammat User rank is Second Lieutenant (5000 - 10000 Reputation Level)shammat User rank is Second Lieutenant (5000 - 10000 Reputation Level)shammat User rank is Second Lieutenant (5000 - 10000 Reputation Level)shammat User rank is Second Lieutenant (5000 - 10000 Reputation Level)shammat User rank is Second Lieutenant (5000 - 10000 Reputation Level)shammat User rank is Second Lieutenant (5000 - 10000 Reputation Level)shammat User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 15 h 15 m 49 sec
Reputation Power: 57
Try this
Code:
SELECT name
FROM things
GROUP BY name
HAVING count(*) = (SELECT max(num_things)
                   FROM (SELECT name, count(*) AS num_things 
                         FROM things GROUP BY name) AS m);

Last edited by shammat : April 21st, 2008 at 02:47 AM. Reason: corrected missing alias for subselect

Reply With Quote
  #5  
Old April 21st, 2008, 03:52 AM
trickma trickma is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2008
Posts: 4 trickma User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 56 m 34 sec
Reputation Power: 0
Spot on shammat, thank-you so much for that

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesPostgreSQL Help > Simple query help! **PostgreSQL


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway