Description
TheIf combinator can be applied to the sum
function to calculate the sum of values for rows where the condition is true,
using the sumIf aggregate combinator function.
Example usage
In this example, we’ll create a table that stores sales data with success flags, and we’ll usesumIf to calculate the total sales amount for successful transactions.
Query
sumIf function will sum only the amounts where is_successful = 1.
In this case, it will sum: 100.50 + 200.75 + 300.00 + 175.25.
Response
Calculate trading volume by price direction
In this example we’ll use thestock table available at ClickHouse playground
to calculate trading volume by price direction in the first half of the year 2002.
Query
Calculate trading volume by stock symbol
In this example we’ll use thestock table available at ClickHouse playground
to calculate trading volume by stock symbol in 2006 for three of the largest tech
companies at the time.
Query
Response