Welcome to WebmasterWorld Guest from 54.167.82.170

Forum Moderators: open

Message Too Old, No Replies

Needed SQL Query to count fields

     

mamylo

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

5+ Year Member



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

topr8

7:45 am on Sep 8, 2010 (gmt 0)

WebmasterWorld Senior Member topr8 is a WebmasterWorld Top Contributor of All Time 10+ Year Member



SELECT COUNT(*) as counter,value FROM test GROUP BY value ORDER BY value;

LifeinAsia

3:37 pm on Sep 8, 2010 (gmt 0)

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



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.
 

Featured Threads

Hot Threads This Week

Hot Threads This Month