Aggregate Function on Uniqueidentifier (GUID) Aggregate Function on Uniqueidentifier (GUID) sql-server sql-server

Aggregate Function on Uniqueidentifier (GUID)


Just cast it as a BINARY(16).

SELECT category, MIN(CAST(guid AS BINARY(16)))FROM myTableGROUP BY category

You can cast it back later if necessary.

WITH CategoryValueAS(        SELECT category, MIN(CAST(guid AS BINARY(16)))    FROM myTable    GROUP BY category)SELECT category, CAST(guid AS UNIQUEIDENTIFIER)FROM CategoryValue


Assuming you're using SQL Server 2005 or later:

;with Numbered as (     select category,guid,ROW_NUMBER() OVER (PARTITION BY category ORDER BY guid) rn     from myTable)select * from Numbered where rn=1


Aggregate functions can be used on Uniqueidentifier columns if SQL Server Version >= 2012

expression

Is a constant, column name, or function, and any combination of arithmetic, bitwise, and string operators. MIN can be used with numeric, char, varchar, uniqueidentifier, or datetime columns, but not with bit columns. Aggregate functions and subqueries are not permitted.