Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

SQL Row Counting

  • 17-09-2009 05: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, Registered Users 2 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, Registered Users 2 Posts: 23,202 ✭✭✭✭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