Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Jun 2009
    Posts
    144
    Thanks
    3
    Thanked 20 Times in 20 Posts

    Group by troubles

    I am wanting to use the power of MYSQL to retrieve the data versus PHP counting.

    So I have a table that contains two columns

    ### myDate ### myClient ###
    5/10/13--------101
    6/10/13--------102
    5/10/13--------101
    5/10/13--------101
    5/10/13--------102
    6/10/13--------103
    6/10/13--------101
    #####################

    What I want it to ouput is one copy of each day and count the number of unique clients attached to those days. I have tried using

    PHP Code:
    SELECT myDateCOUNTdate FROM sometable GROUP BY myDatemyClient 
    This is not working, I have tried the DISTINCT function but still cant get it to release the correct data. Any help much appreciated. Based on the example data, in my mind what I should see is

    ###myDate###count###
    5/10/13--------2-------- (a unique value of 101, and 102)
    6/10/13--------3--------(a unique value of 101,102 and 103)

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,458
    Thanks
    76
    Thanked 4,373 Times in 4,338 Posts
    Trivial in MySQL (and SQL Server, not so easy in some other DBs).

    Code:
    SELECT myDate, COUNT( DISTINCT myClient )
    FROM tablename
    GROUP BY myDate
    ORDER BY myDate
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    Regular Coder
    Join Date
    Jun 2009
    Posts
    144
    Thanks
    3
    Thanked 20 Times in 20 Posts
    Trivial, I love trivial! I find that incredible given the power of such a function. By the way your code works perfectly, thank you.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,458
    Thanks
    76
    Thanked 4,373 Times in 4,338 Posts
    MySQL has quite a few surprises like that. Things that are quite hard to do in other DBs that it makes easy. Look into GROUP_CONCAT, for example. Amazingly powerful.

    On the other hand, it has some holes (especially in how capably it uses indexes) that you can fly 747's through.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •