SitePoint Sponsor

User Tag List

Results 1 to 12 of 12

Thread: count and grouping

  1. #1
    SitePoint Member
    Join Date
    Nov 2008
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    count and grouping

    Hi all, Please help if you are able to, with the problem I’m having below.
    I need to find all 1’s in Col3 grouped by the value in Col2. I also need to know the total number of rows for the value in Col2.

    Col1 Col2 Col3
    ABC 1234 2
    ABC 1234 2
    ABC 5678 2
    ABC 5678 1

    Desired Output
    eg
    Col1 Col2 Total 1’s Total Rows (Col2)
    ABC 1234 0 2
    ABC 5678 1 2

    I’ve tried various queries with count and grouping but it doesn’t give me the output I need.
    Thanks

  2. #2
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,849
    Mentioned
    16 Post(s)
    Tagged
    1 Thread(s)
    That is an sql question, if you try asking it in the sql or mysql forum then you will likely get a fuller answer.

  3. #3
    SitePoint Member
    Join Date
    Nov 2008
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    My bad...
    Can this be moved...or should i create a new thread in the MySQL section

  4. #4
    SitePoint Member
    Join Date
    Nov 2008
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    count and grouping

    Hi all, Please help if you are able to, with the problem I’m having below.
    I need to find all 1’s in Col3 grouped by the value in Col2. I also need to know the total number of rows for the value in Col2.

    Col1 Col2 Col3
    ABC 1234 2
    ABC 1234 2
    ABC 5678 2
    ABC 5678 1

    Desired Output
    eg
    Col1 Col2 Total 1’s Total Rows(Col2)
    ABC 1234 0___________ 2
    ABC 5678 1 ___________ 2

    I’ve tried various queries with count and grouping but it doesn’t give me the output I need.
    Thanks
    Last edited by nu2php; Nov 3, 2008 at 02:23. Reason: oops

  5. #5
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,849
    Mentioned
    16 Post(s)
    Tagged
    1 Thread(s)
    There are some of the sql guys that find the sql questions in here, but mostly you'll get a PHPer giving it their best shot.

  6. #6
    SitePoint Guru
    Join Date
    Jan 2004
    Location
    Uppsala, sverige
    Posts
    690
    Mentioned
    2 Post(s)
    Tagged
    1 Thread(s)
    Code:
    select col1,
             col2,
             sum(case when col3 = 1 then 1 else 0 end),
             count(*)
      from t
     group by col1,col2

  7. #7
    SitePoint Member
    Join Date
    Nov 2008
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    WOW thanks a million.
    It works perfectly.
    I was going nuts using count and group and every other thing i could think off.
    Thanks again

  8. #8
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,359
    Mentioned
    29 Post(s)
    Tagged
    0 Thread(s)
    I'm not a MySQL expert by any stretch of the imagination, so the following might be horribly inefficient (or just plain not work!):

    Code SQL:
    SELECT
    	t1.Col1, t1.Col2, 
    	COUNT(t2.Col3) AS `Total 1's`,
    	COUNT(t1.Col2) AS `Total Rows (Col2)`
    FROM 
    	my_table AS t1
    LEFT JOIN
    	(SELECT Col3 FROM my_table WHERE Col3 = 1)
    	AS t2 ON t2.Col3 = t1.Col3
    GROUP BY
    	t1.Col2

    Edit: Must learn to wake up before thinking about SQL... thanks r937 for making me remember CASE
    Salathe
    Software Developer and PHP Documentation Team.

  9. #9
    SQL Consultant silver trophybronze trophy
    SitePoint Award Recipient r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    38,456
    Mentioned
    34 Post(s)
    Tagged
    1 Thread(s)
    Code:
    SELECT Col1
         , Col2
         , SUM(CASE WHEN Col3 = 1
                    THEN 1 ELSE 0 END) AS Total_1s
         , COUNT(*)                    AS Total_Rows
      FROM my_table
    GROUP
        BY Col1
         , Col2
    an alternative that is just a wee bit more obscure --
    Code:
    SELECT Col1
         , Col2
         , COUNT(*) - 
                 COUNT(NULLIF(Col3,1)) AS Total_1s
         , COUNT(*)                    AS Total_Rows
      FROM my_table
    GROUP
        BY Col1
         , Col2
    r937.com | rudy.ca | Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  10. #10
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,109
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    .....must......hire.......Rudy.....
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  11. #11
    SitePoint Member
    Join Date
    Nov 2008
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up

    Thanks for the help guys.
    much appreciated

  12. #12
    SQL Consultant silver trophybronze trophy
    SitePoint Award Recipient r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    38,456
    Mentioned
    34 Post(s)
    Tagged
    1 Thread(s)
    see what happens when you re-post in a separate forum? we end up having to merge the threads anyway, and then the interleaving of replies gets all jumbled

    please, if you've posted in the wrong forum, push the little red triangle and ask that the thread be moved

    r937.com | rudy.ca | Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

Bookmarks

Posting Permissions

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