You can use a CASE statement:

SELECT count(id), 
    SUM(hour) as totHour, 
    SUM(case when kind = 1 then 1 else 0 end) as countKindOne
Answer from Taryn on Stack Overflow
๐ŸŒ
MySQL Tutorial
mysqltutorial.org โ€บ home โ€บ mysql aggregate functions โ€บ mysql sum if
MySQL SUM IF
October 10, 2023 - First, the SUM function calculates ... if the year is 2023. The query returns the total sales amount for Phone in October 2023. Use the MySQL SUM IF to perform a conditional summation....
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ mysql-sum-query-with-if-condition
MySQL - SUM() Function
July 30, 2019 - If you use the DISTINCT keyword, this function calculates and returns the sum of the unique values of the given column. The SUM() function ignores the NULL values in the calculation. Following is the syntax of MySQL SUM() function โˆ’
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ mysql โ€บ conditional-summation
Conditional Summation - GeeksforGeeks
July 23, 2025 - Conditional summation in MySQL calculates the total of column values based on specific conditions, utilizing features like the CASE statement or WHERE clause to filter rows before performing summation.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ article โ€บ mysql-sum-query-with-if-condition-using-stored-procedure
MySQL Sum Query with IF Condition using Stored Procedure
mysql> delimiter // mysql> create procedure sp_GetSumWithPaymentMode11(PaymentMode varchar(200)) โˆ’> begin โˆ’> select PaymentMode,sum(if(ModeOfPayment=PaymentMode,Amount,0)) as TotalAmount from SumWithIfCondition; โˆ’> end // Query OK, 0 rows affected (0.32 sec) mysql> delimiter ;
๐ŸŒ
MySQL Tutorial
mysqltutorial.org โ€บ home โ€บ mysql control flow functions and expressions โ€บ mysql if function
MySQL IF Function
April 11, 2020 - In the query above, if the order status is shipped or cancelled , the IF function returns 1 otherwise it returns 0. The SUM function calculates the total number of shipped and cancelled orders based on the returned value of the IF function.
๐ŸŒ
w3resource
w3resource.com โ€บ mysql โ€บ aggregate-functions-and-grouping โ€บ aggregate-functions-and-grouping-sum().php
MySQL SUM() function
March 2, 2026 - MySQL SUM() function retrieves the sum of a unique value of an expression if it is accompanied by DISTINCT clause. The following MySQL statement returns the sum of a number of branches ('no_of_branch') from publisher table, where, if more than ...
Find elsewhere
๐ŸŒ
DataCamp
datacamp.com โ€บ doc โ€บ mysql โ€บ mysql-sum
MySQL SUM() Function: Usage & Examples
In this syntax, `SUM(column_name)` sums up the values of `column_name` from the specified table or subset of rows.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ can-i-use-sum-with-if-in-mysql
Can I use SUM() with IF() in MySQL?
July 30, 2019 - mysql> SELECT SUM(IF(Value=100, 1, 0) + IF(Value2=100, 1, 0)) as Hundred, SUM(IF(Value=400, 1, 0) + IF(Value2=400, 1, 0)) as FourHundred FROM DemoTable;
๐ŸŒ
Modern SQL
modern-sql.com โ€บ excel โ€บ sumif-in-sql
SUMIF in SQL: SUM(CASE WHEN <condition> THEN <value> END)
The Excel function SUMIF can be implemented in SQL Server, Oracle, MySQL, MariaDB and others using a CASE expression.
๐ŸŒ
Latenode
community.latenode.com โ€บ other questions โ€บ mysql
How to use conditional SUM in MySQL with IF statement - MySQL - Latenode Official Community
June 24, 2025 - I have a table with the following structure: product_id | category | amount 10 | type_x | 300 11 | type_x | 250 12 | type_y | 150 Iโ€™m trying to get separate totals for each category using this query: SELECT IF(category = 'type_x', SUM(amount), 0) AS type_x_total, IF(category = 'type_y', ...
๐ŸŒ
W3Schools
w3schools.com โ€บ sql โ€บ func_mysql_sum.asp
MySQL SUM() Function
The SUM() function calculates the sum of a set of values. Note: NULL values are ignored. ... Coding fundamentals as a game. Bite-sized lessons and challenges. ... Ready to start your journey?
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ mysql sum
MySQL SUM() Function - Scaler Topics
May 18, 2023 - MySQL SUM() function returns the sum of an expression. SUM() function returns NULL when the return set has no rows.
๐ŸŒ
EDUCBA
educba.com โ€บ home โ€บ data science โ€บ data science tutorials โ€บ mysql tutorial โ€บ mysql sum()
MySQL sum() | Complete Guide to MySQL sum() with Query Examples
June 6, 2023 - The result sum value evaluates based on a condition provided by the values in the next table. The MySQL SUM() is similar to a mathematical sum calculation that finds the totality of provided table values in the database.
Address: Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
๐ŸŒ
OneUptime
oneuptime.com โ€บ home โ€บ blog โ€บ how to use the sum() function in mysql
How to Use the SUM() Function in MySQL
March 31, 2026 - For running totals and cumulative sums, use SUM() OVER (ORDER BY ...) as a window function in MySQL 8.0+. You can also sum computed expressions like quantity * price directly inside SUM().
๐ŸŒ
MySQL Tutorial
mysqltutorial.org โ€บ home โ€บ mysql aggregate functions โ€บ mysql sum() function
MySQL SUM Function
November 10, 2023 - The syntax of the SUM() function is as follows: SUM(DISTINCT expression)Copy ยท Here is how the SUM() function works: If you use the SUM() function in a SELECT statement that returns no row, the SUM() function returns NULL, not zero.
๐ŸŒ
MySQL
forums.mysql.com โ€บ read.php
MySQL :: SUM(IF())
December 9, 2009 - I am trying to construct a table of summaries similar to the awful Excel "PivotTable". I currently usethe SUM(IF(expr1, 1, 0)) technique to generate counts filtered by a GROUP BY clause. Can anyone tell me how I might achieve the same thing, but with a number of conditions instead of expr1???
๐ŸŒ
Codemia
codemia.io โ€บ home โ€บ knowledge hub โ€บ mysql is it possible to 'sum if' or to 'count if'?
MySql is it possible to 'SUM IF' or to 'COUNT IF'? | Codemia
September 23, 2025 - MySQL does not expose built in functions named SUM_IF or COUNT_IF, but you can express the same behavior with conditional aggregation. This pattern is one of the most useful SQL techniques for dashboards, billing summaries, and operational metrics.