Skip to main content

GROUP BY

Use GROUP BY to break aggregated results down by dimensions. Grouped dimensions shape the rows returned by a query. Use time dimensions for time-based groups, or use TOP to limit dimensions with many possible values.

Syntax

GROUP BY item[, ...]

item:
dimension
[ONLY] TOP count dimension [OVERALL]

GROUP BY uses dimensions, which are fields defined by the schema selected in FROM. Available dimensions vary by schema. Group by one dimension, or list multiple comma-separated dimensions in the order that ShopifyQL should apply the grouping.

A dimension returned by SHOW must appear in GROUP BY, unless it's a time dimension represented by TIMESERIES. If you rename a SHOW dimension with AS, then use the name after AS in GROUP BY. For multiple-schema queries, use dimensions that the selected schemas can group together in a multi-fact query.

Examples

ShopifyQL

FROM payments
SHOW net_payments
GROUP BY payment_method WITH TOTALS
SINCE -30d UNTIL today
ORDER BY net_payments DESC
LIMIT 10

Group aggregated results by time dimensions, including intervals such as day and month, and date or clock values such as day_of_week and hour_of_day. GROUP BY returns only values that exist in the data for a grouped time dimension. Use TIMESERIES when the result should include values for one time dimension even when those values have no data.

Anchor to second
second
yyyy-MM-ddThh:mm:ss

Groups rows into one-second intervals.

Anchor to minute
minute
yyyy-MM-ddThh:mm

Groups rows into one-minute intervals.

yyyy-MM-ddThh

Groups rows into one-hour intervals.

yyyy-MM-dd

Groups rows into calendar days.

yyyy-MM-dd

Groups rows into calendar weeks.

Anchor to month
month
yyyy-MM-dd

Groups rows into calendar months.

Anchor to quarter
quarter
yyyy-MM-dd

Groups rows into calendar quarters.

yyyy-MM-dd

Groups rows into calendar years.

Anchor to hour_of_day
hour_of_day
0-23

Groups rows by hour of the day, combining that hour across all dates.

Anchor to day_of_week
day_of_week
0-6

Groups rows by day of the week, combining that weekday across all dates.

Anchor to week_of_year
week_of_year
1-53

Groups rows by week of the year, combining that week across years.

Anchor to month_of_year
month_of_year
1-12

Groups rows by month of the year, combining that month across years.

Examples

ShopifyQL

FROM sales
SHOW total_sales
GROUP BY month
SINCE startOfYear(0y) UNTIL today
ORDER BY month ASC

Use TOP to limit a high-cardinality dimension, such as product_title or shipping_country, to its most significant values. TOP count returns the count highest-ranked values, ranked by the metric the query aggregates. count must be a positive integer.

By default, ShopifyQL collects the remaining values into a single Other row, so the results still add up to the query's total. Optional keywords change this behavior:

  • ONLY, placed before TOP, drops the Other row and returns only the top values.
  • OVERALL, placed after the dimension, ranks values across the entire result instead of within each preceding GROUP BY dimension. It has no effect on the first dimension.

You can combine TOP with plain dimensions and use more than one TOP in a single GROUP BY.

Syntax

GROUP BY [ONLY] TOP count dimension [OVERALL][, ...]
Examples

ShopifyQL

FROM sales
SHOW total_sales
GROUP BY TOP 10 product_title
DURING last_month
ORDER BY total_sales DESC

Was this page helpful?