This time we’re going bigger than ever. Fabric, Power BI, SQL, AI and more. We're covering it all. You won't want to miss it.
Learn moreDid you hear? There's a new SQL AI Developer certification (DP-800). Start preparing now and be one of the first to get certified. Register now
Fabric Data Warehouse now supports the ANY_VALUE() aggregate, making it easier to write readable, efficient T-SQL when you want to group by a key but still return descriptive columns that are functionally the same for every row in the group.
The following is a practical example to see how ANY_VALUE() simplifies common aggregation scenarios. Suppose you want to calculate the total revenue from taxi trips for each geographic area (specifically grouping by GeographyID). At the same time, you’d like to include descriptive details such as the associated city, state, and country for each group. Since these descriptive columns are functionally dependent on GeographyID and don’t vary within each group, ANY_VALUE() allows you to easily project them in your results without cluttering your GROUP BY clause or resorting to less intuitive aggregates.
SQL_query_showing_total_revenue_by_geography._Query_returns_the_values_for_city
Figure: Using ANY_VALUE() to project descriptive columns while aggregating trips by GeographyID.
Use ANY_VALUE() when the chosen column is functionally dependent on the grouping key (for example, City is constant for a given GeographyID). If the values can legitimately vary within the group, pick an aggregate that matches the business rule (for example, MIN(), MAX(), or a windowing approach).
Adopting this pattern offers several important advantages when writing aggregation queries. Consider the following key benefits:
For syntax, examples, and additional details, refer to the ANY_VALUE (Transact-SQL).
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.