|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
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
|
|||
|
|||
|
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 ![]() |
|
#2
|
|||
|
|||
|
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) |
|
#3
|
|||
|
|||
|
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 |
|
#4
|
|||
|
|||
|
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 |
|
#5
|
|||
|
|||
|
Spot on shammat, thank-you so much for that
![]() |
![]() |
| Viewing: Dev Shed Forums > Databases > PostgreSQL Help > Simple query help! **PostgreSQL |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|