sql – 如何做一个区分大小写GROUP BY?

前端之家收集整理的这篇文章主要介绍了sql – 如何做一个区分大小写GROUP BY?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
如果我执行下面的代码
with temp as
(
  select 'Test' as name
  UNION ALL
  select 'TEST'
  UNION ALL
  select 'test'
  UNION ALL
  select 'tester'
  UNION ALL
  select 'tester'
)
SELECT name,COUNT(name)
FROM temp
group by name

它返回结果:

TEST   3
tester 2

有没有办法让小组区分大小写,结果将是:

Test   1
TEST   1
test   1
tester 2

解决方法

您可以使用区分大小写的排序规则:
with temp as
(
  select 'Test' COLLATE Latin1_General_CS_AS as name
  UNION ALL
  select 'TEST'
  UNION ALL
  select 'test'
  UNION ALL
  select 'tester'
  UNION ALL
  select 'tester'
)
SELECT name,COUNT(name)
FROM temp
group by name

猜你在找的MsSQL相关文章