Welcome to WebmasterWorld Guest from 54.158.119.60

Forum Moderators: open

Message Too Old, No Replies

Needed SQL Query to count fields

     
10:09 pm on Sep 7, 2010 (gmt 0)

New User

5+ Year Member

joined:Dec 25, 2009
posts: 5
votes: 0


Hi
I have a table named test with two fields: ID and value
And I insert these records:


ID value
1 a
2 b
3 a
4 a
5 c
6 b
7 a
8 d



I want to write an SQL query to get this result:


a b c d
4 2 1 1

How can I get this result?

Note: I know I can use COUNT with GROUP BY, but I want to get this data with only 1 fetching.

Thanks in advance
7:45 am on Sept 8, 2010 (gmt 0)

Senior Member

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

joined:Apr 19, 2002
posts:3230
votes: 17


SELECT COUNT(*) as counter,value FROM test GROUP BY value ORDER BY value;
3:37 pm on Sept 8, 2010 (gmt 0)

Moderator from US 

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

joined:Dec 10, 2005
posts:5635
votes: 53


The OP wants the results displayed as:
a b c d
4 2 1 1

Not as:
a 4
b 2
c 1
d 1

What DB are you using? Certain DBs may have a proprietary function to do something like that.
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members