Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using IF in BigQuery SQL

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

like image 797
Sema196 Avatar asked Mar 02 '16 05:03

Sema196


People also ask

Can we use loop in BigQuery?

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!

WHAT IS WITH clause in BigQuery?

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.

What is coalesce in BigQuery?

COALESCE(expr[, ... ]) Description. Returns the value of the first non-null expression. The remaining expressions are not evaluated.

How to use if statement in BigQuery?

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.

What is the difference between BigQuery and query function?

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

How do I write SQL in BigQuery?

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:

What is a BigQuery script?

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.


2 Answers

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    
like image 94
Mikhail Berlyant Avatar answered Sep 17 '22 14:09

Mikhail Berlyant


I would use COUNTIF, just like @justbeez sugests. According to documentation

Returns the count of TRUE values for expression

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.

like image 37
dinigo Avatar answered Sep 20 '22 14:09

dinigo