Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

SQL Row Counting

Options
  • 17-09-2009 5:37pm
    #1
    Closed Accounts Posts: 448 ✭✭


    My head is fried and I can't seem to think of a way to achieve the following in a single query.

    I have the following tableA

    | id | value |
    | 1 | A |
    | 2 | A |
    | 3 | B |
    | 4 | A |
    | 5 | B |
    | 6 | A |
    | 7 | B |

    How can I use SQL count to return:
    A: 4
    B: 3

    Where the number is the frequency of occurance for a particular value. I have been looking at the count function and examples thereof, and my head keeps saying "Blah!".

    Appreciate any pointers you can offer.


Comments

  • Registered Users Posts: 515 ✭✭✭NeverSayDie


    This should get you in the right direction;
    http://www.w3schools.com/sql/sql_groupby.asp


  • Closed Accounts Posts: 448 ✭✭ve


    /slaps own head :rolleyes:

    Group By, but of course...
    select count(value) from TableA group by value
    

    Oh my head is not working today.

    Cheers ;)


  • Registered Users Posts: 23,212 ✭✭✭✭Tom Dunne


    ve wrote: »
    /slaps own head :rolleyes:

    Group By, but of course...
    select count(value) from TableA group by value
    
    Oh my head is not working today.

    Cheers ;)

    That will only return the following:

    4
    3

    You need to add in the VALUE column:

    SELECT VALUE, count(value) from TableA group by value

    /pedantic :)


  • Closed Accounts Posts: 448 ✭✭ve


    LOL, don't worry. I was just trying to keep the focus of my problem as concise as possible. I have it all working in place now.
    My table wasn't actually called TableA, or the column called value. It was merely created to illustrate the problem at hand. Muhahahahaha. /runs ;)

    Thanks for all the help folks.


Advertisement