SitePoint Sponsor

User Tag List

Results 1 to 8 of 8

Thread: conditional COUNT in MySQL

  1. #1
    SitePoint Guru gavwvin's Avatar
    Join Date
    Nov 2004
    Location
    Cornwall, UK
    Posts
    686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    conditional COUNT in MySQL

    I've got a big query where I SELECT SUM(column) which is grouped by another column... I also want to select the number of rows that equal a certain value. I don't want to exclude those not matching from the sum function (so its not just a WHERE clause) I just want to COUNT if they are equal to a particular value (rather like COUNTIF in excel). Is there a built in SQL function for this? Otherwise I'll have to do a separate query for each one and do mysql_num_rows, which would take extra time.

    Thanks

  2. #2
    SQL Consultant silver trophybronze trophy
    SitePoint Award Recipient r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    38,458
    Mentioned
    34 Post(s)
    Tagged
    1 Thread(s)
    lost in space

    could you give an example of what you're doing and what you want?
    r937.com | rudy.ca | Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  3. #3
    SitePoint Guru
    Join Date
    Jan 2004
    Location
    Uppsala, sverige
    Posts
    690
    Mentioned
    2 Post(s)
    Tagged
    1 Thread(s)
    Code:
    sum(case when <yourCondition> then <yourColumn> else 0 end)

  4. #4
    SitePoint Guru gavwvin's Avatar
    Join Date
    Nov 2004
    Location
    Cornwall, UK
    Posts
    686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks swampboogie, works fine as I want to count when the value is one... but if I need any other number I just have to divide by that number at the end to get the real amount.
    e.g. sum(case when column=3 then column/3 else 0 end) gives the number of rows which have the value 3

  5. #5
    SQL Consultant silver trophybronze trophy
    SitePoint Award Recipient r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    38,458
    Mentioned
    34 Post(s)
    Tagged
    1 Thread(s)
    could you give an example of what you're doing and what you want?
    r937.com | rudy.ca | Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  6. #6
    SitePoint Enthusiast Powerlord's Avatar
    Join Date
    May 2003
    Location
    Mason, MI, USA
    Posts
    37
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by gavwvin
    I've got a big query where I SELECT SUM(column) which is grouped by another column... I also want to select the number of rows that equal a certain value. I don't want to exclude those not matching from the sum function (so its not just a WHERE clause) I just want to COUNT if they are equal to a particular value (rather like COUNTIF in excel). Is there a built in SQL function for this? Otherwise I'll have to do a separate query for each one and do mysql_num_rows, which would take extra time.

    Thanks
    MySQL does have an IF statement, and it looks like it can be used in COUNT statements, like so:
    Code:
    SELECT COUNT(IF(column=3,1,NULL)) AS Count
    FROM table GROUP BY othercolumn
    I used the number 3 as an example.

    I haven't actually tested this SQL, though.

    Edit: Mysql's IF() works like this:
    IF(expression, return this if expression is true, return this if expression is false)
    Ross Bemrose,
    Independant MySQL/Perl/PHP Developer

  7. #7
    SQL Consultant silver trophybronze trophy
    SitePoint Award Recipient r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    38,458
    Mentioned
    34 Post(s)
    Tagged
    1 Thread(s)
    CASE would be better, because it's standard sql, and is portable to other databases

    SELECT COUNT(case when column=3 then 937 else NULL end)) AS Count
    FROM table GROUP BY othercolumn

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

  8. #8
    SitePoint Guru gavwvin's Avatar
    Join Date
    Nov 2004
    Location
    Cornwall, UK
    Posts
    686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks everyone, I've learnt something today!

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
  •