Group by

In this SQL query,

SELECT
    flights.distance  AS flights_distance,
    COUNT(*) AS flights_count
FROM `cloud-training-demos.looker_flights.flights`  AS flights
GROUP BY
    1
ORDER BY
    2 DESC

what is meant by GROUP BY 1? Does 1 refer to flight_distance field?

Solved Solved
0 1 614
1 ACCEPTED SOLUTION

what is meant by GROUP BY 1? Does 1 refer to flight_distance field?

Yes.

In many SQL dialects, you can `GROUP BY` (and `ORDER BY`) the position of the field as it appears in the `SELECT` clause.  This is a convenient short-hand notation that helps when you need to group-by expressions that are fairly long and cumbersome to copy from `SELECT` to the `GROUP BY` clause of the query.

View solution in original post

1 REPLY 1

what is meant by GROUP BY 1? Does 1 refer to flight_distance field?

Yes.

In many SQL dialects, you can `GROUP BY` (and `ORDER BY`) the position of the field as it appears in the `SELECT` clause.  This is a convenient short-hand notation that helps when you need to group-by expressions that are fairly long and cumbersome to copy from `SELECT` to the `GROUP BY` clause of the query.

Top Labels in this Space