Función de agregado en Uniqueidentifier (GUID)


Digamos que tengo la siguiente tabla:

category | guid
---------+-----------------------
   A     | 5BC2...
   A     | 6A1C...
   B     | 92A2...

Básicamente, quiero hacer el siguiente SQL:

SELECT category, MIN(guid)
  FROM myTable
 GROUP BY category

No necesariamente tiene que ser MIN. Solo quiero devolver un GUID de cada categoría. No me importa cuál. Desafortunadamente, SQL Server no permite MIN o MAX en GUID.

Por supuesto, podría convertir el guid en un varchar, o crear un TOP 1 SQL anidado, pero eso parece una solución fea. ¿Hay alguna solución elegante que me he perdido?

Author: Heinzi, 2011-05-20

5 answers

Asumiendo que está usando SQL Server 2005 o posterior:

;with Numbered as (
     select category,guid,ROW_NUMBER() OVER (PARTITION BY category ORDER BY guid) rn
     from myTable
)
select * from Numbered where rn=1
 31
Author: Damien_The_Unbeliever,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-05-20 08:40:02

Simplemente lanzarlo como un BINARY(16).

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

Puedes echarlo hacia atrás más tarde si es necesario.

WITH CategoryValue
AS
(    
    SELECT category, MIN(CAST(guid AS BINARY(16)))
    FROM myTable
    GROUP BY category
)
SELECT category, CAST(guid AS UNIQUEIDENTIFIER)
FROM CategoryValue
 45
Author: K Biel,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-03-03 11:10:42

Las funciones de agregado se pueden usar en columnas Uniqueidentifier si SQL Server Version > = 2012

Expresión

Es una constante, nombre de columna o función, y cualquier combinación de operadores aritméticos, bit a bit y de cadena. MIN se puede utilizar con columnas numéricas, char, varchar, uniqueidentifier o datetime, pero no con columnas de bits. Funciones agregadas y subconsultas no son permitir.

 14
Author: doerig,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-08-26 14:00:39
declare @T table(category char(1), guid uniqueidentifier) 

insert into @T 
select 'a', newid() union all
select 'a', newid() union all
select 'b', newid()

select
  S.category,
  S.guid
from
(  
  select
    T.category,
    T.guid,
    row_number() over(partition by T.category order by (select 1)) as rn
  from @T as T
) as S
where S.rn = 1

Si está en SQL Server 2000, podría hacerlo

select 
  T1.category,
  (select top 1 T2.guid 
   from @T as T2
   where T1.category = T2.category) as guid
from @T as T1
group by T1.category   
 4
Author: Mikael Eriksson,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-05-20 08:42:52

SELECCIONE la categoría top 1, guid DE MyTable GRUPO POR categoría, guid

 -3
Author: Abhi,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-11-16 09:20:21