Adding other aggregation criteria
You can aggregate data by both column values and expressions.
For example:
- Group taxi rides by long distance.
- Group taxi rides by short distance.
- Calculate the total number of grouped rides.
SELECT
distance_type,
COUNT(*)
FROM
`tutorial-analytics`
GROUP BY -- CASE checks a list of conditions
CASE -- and returns one of several possible
WHEN trip_distance > 10 -- expressions or an expression with any operator
THEN "long" -- that supports the expression in question. For example, you can use CASE
ELSE "short" -- in SELECT expressions
END AS distance_type -- and with operators IN, WHERE, and ORDER BY.
-- You can run GROUP BY
-- for any expression.
-- The result is available in SELECT
-- via an alias specified using AS.
View the example in the right-hand section and click
Query results are available in the Result tab as a table or schema.