Aggregate Function on Uniqueidentifier (GUID)

后端 未结 5 1385
日久生厌
日久生厌 2021-02-02 05:41

Let\'s say I have the following table:

category | guid
---------+-----------------------
   A     | 5BC2...
   A     | 6A1C...
   B     | 92A2...
5条回答
  •  清酒与你
    2021-02-02 06:35

    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
    

    If you are on SQL Server 2000 you could to this

    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   
    

提交回复
热议问题