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 Top answer 1 of 6
235
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
2 of 6
107
you want something like:
SELECT count(id), SUM(hour) as totHour, SUM(kind=1) as countKindOne;
Note that your second example was close, but the IF() function always takes three arguments, so it would have had to be COUNT(IF(kind=1,1,NULL)). I prefer the SUM() syntax shown above because it's concise.
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....
03:57
What is SUM () Function in MySQL? How to Work & Use SUM() Function ...
02:16
MySQL SUM() Function - YouTube
02:31
MySQL SUM IF - YouTube
05:38
quicktip mysql sum if - YouTube
MySQL SUM IF
11:29
Rollup in SQL | SubTotal | GrandTotal | SumIf | MySQL | Pivots ...
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 โ
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 ;
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.
Call: +917738666252
Address: Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
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.