唯一标识符 (GUID) 上的聚合函数

发布于 2024-11-08 18:57:30 字数 440 浏览 0 评论 0原文

假设我有下表:

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

基本上,我想要执行以下 SQL:

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

它不一定必须是 MIN。我只想返回每个类别的一个 GUID。我不在乎是哪一个。不幸的是,SQL Server 不允许在 GUID 上使用 MIN 或 MAX。

当然,我可以将 guid 转换为 varchar,或者创建一些嵌套的 TOP 1 SQL,但这似乎是一个丑陋的解决方法。我错过了一些优雅的解决方案吗?

Let's say I have the following table:

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

Basically, I want to do the following SQL:

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

It doesn't necessarily have to be MIN. I just want to return one GUID of each category. I don't care which one. Unfortunately, SQL Server does not allow MIN or MAX on GUIDs.

Of course, I could convert the guid into a varchar, or create some nested TOP 1 SQL, but that seems like an ugly workaround. Is there some elegant solution that I've missed?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

千仐 2024-11-15 18:57:30

只需将其转换为 BINARY(16) 即可。

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

如果需要,您可以稍后将其投射回来。

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

Just cast it as a BINARY(16).

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

You can cast it back later if necessary.

WITH CategoryValue
AS
(    
    SELECT category, MIN(CAST(guid AS BINARY(16)))
    FROM myTable
    GROUP BY category
)
SELECT category, CAST(guid AS UNIQUEIDENTIFIER)
FROM CategoryValue
音盲 2024-11-15 18:57:30

假设您使用的是 SQL Server 2005 或更高版本:

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

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
千纸鹤 2024-11-15 18:57:30

如果 SQL Server 版本 >= 2012,则可以在 Uniqueidentifier 列上使用聚合函数

表达

是常量、列名或函数,以及它们的任意组合
算术、位和字符串运算符。 MIN 可与
numeric、char、varchar、uniqueidentifier 或 datetime 列,但不包括
与位列。聚合函数和子查询不是
允许。

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.

泛泛之交 2024-11-15 18:57:30
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

如果您使用的是 SQL Server 2000,您可以这样做

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   
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   
携余温的黄昏 2024-11-15 18:57:30

选择前 1 个类别、指南
来自我的表
按类别、指南分组

SELECT top 1 category, guid
FROM myTable
GROUP BY category,guid

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文