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 the 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 in
ELSE "short" -- SELECT expressions
END AS distance_type -- and with operators like IN, WHERE, and ORDER BY.
-- You can run GROUP BY
-- for any expression.
-- 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.