Welcome to WebmasterWorld Guest from 54.146.233.30

Forum Moderators: open

Message Too Old, No Replies

SQL to return counts of values

     
10:52 pm on Jul 16, 2007 (gmt 0)

Junior Member

5+ Year Member

joined:Jan 10, 2007
posts:65
votes: 0


I'm trying to accomplish this with one DB call if possible.

I've got a table with a "where did you hear about us" column, the contents are predetermined and are for example "google, friend, word of mouth, other".

Is there a way to return a count of this column and group the results by answer type?

Eg:
Google (245)
Friend (23)
Word of Mouth (83)
Other (34)

11:05 pm on July 16, 2007 (gmt 0)

Administrator

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
posts:10550
votes: 10


SELECT COUNT(answercolumn), answercolumn FROM tablename GROUP BY answercolumn
7:26 am on July 17, 2007 (gmt 0)

Junior Member

5+ Year Member

joined:Jan 10, 2007
posts:65
votes: 0


Fantastic, thanks!
8:34 am on July 17, 2007 (gmt 0)

Administrator

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
posts:10550
votes: 10


if it's a big enough table you might want an index on "answercolumn".
9:25 pm on July 17, 2007 (gmt 0)

Junior Member

5+ Year Member

joined:Jan 10, 2007
posts:65
votes: 0


It's only around 3000 rows at the moment, so not a huge table by any means.

I don't think this SQL's going to be called more than once a week so it's not going to put a strain on anything but I'd not even considered indexing that column so thanks again!

 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members