homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / Databases
Forum Library, Charter, Moderator: open

Databases Forum

Needed SQL Query to count fields

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

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 Sep 8, 2010 (gmt 0)

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


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

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.

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / Databases
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved