Case: I have Sales table in BQ and item_num column contains values 1, -1 and 0. I want to count how many cases I have for each value.
Tried a simple query below, but count returns exactly the same number for each case.. What I am missing?
SELECT
count(if(item_num > 0,1, 0)) as buysplus,
count(if(item_num < 0,1, 0)) as buysminus,
count(if(item_num = 0,1, 0)) as buyszero
from MyShop.Sales
thanks
Zapier lets you send info between Google BigQuery and Looping by Zapier automatically—no code required. Triggers when a query job has completed processing successfully. automatically do this!
The WITH clause contains one or more common table expressions (CTEs). Each CTE binds the results of a subquery to a table name, which can be used elsewhere in the same query expression. BigQuery does not materialize the results of non-recursive CTEs within the WITH clause.
COALESCE(expr[, ... ]) Description. Returns the value of the first non-null expression. The remaining expressions are not evaluated.
The BigQuery supports IF statement to control the execution. The if statements can perform a particular task based on the certain conditions. For example, execute set of statements based on the variable value There are three forms of IF statement that you can use.
The query function syntax is like so: =query (range, “SELECT * WHERE x = y”) In BigQuery SQL (and most other forms of SQL), the only key difference is that you reference a table (with a FROM parameter), instead of a spreadsheet range: SELECT * FROM table WHERE x = y
If you already know the Google Sheets query function, you’re more than halfway to writing SQL in BigQuery. The query function syntax is like so: =query(range, “SELECT * WHERE x = y”) In BigQuery SQL (and most other forms of SQL), the only key difference is that you reference a table (with a FROM parameter), instead of a spreadsheet range:
In BigQuery, a script is a SQL statement list to be executed in sequence. A SQL statement list is a list of any valid BigQuery statements that are separated by semicolons. BigQuery Control Flow Statements You can use two types of control structures while using scripting in BigQuery.
SELECT
SUM(IF(item_num > 0, 1, 0)) AS buysplus,
SUM(IF(item_num < 0, 1, 0)) AS buysminus,
SUM(IF(item_num = 0, 1, 0)) AS buyszero
FROM MyShop.Sales
or even less verbose version of it:
SELECT
SUM(item_num > 0) AS buysplus,
SUM(item_num < 0) AS buysminus,
SUM(item_num = 0) AS buyszero
FROM MyShop.Sales
this will give yo result like below
buysplus buysminus buyszero
4 2 3
Another option would be transposed version of it
SELECT
item_num AS buys,
COUNT(1) AS volume
FROM MyShop.Sales
GROUP BY 1
with the result as below
buys volume
0 3
1 4
-1 2
I would use COUNTIF
, just like @justbeez sugests. According to documentation
Returns the count of
TRUE
values forexpression
So applied to your case would look like:
SELECT
COUNTIF(item_num > 0) as buysplus,
COUNTIF(item_num < 0) as buysminus,
COUNTIF(item_num = 0) as buyszero
FROM MyShop.Sales
You don't save a lot of code, but count is a little bit more idiomatic than sum IMHO.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With