SQL SERVER - 了解 MIN(text) 的工作原理

发布于 2024-10-08 23:58:04 字数 270 浏览 6 评论 0原文

我正在做一些挖掘并寻找有关 SQL Server 如何评估 MIN(Varchar) 的解释。

我在 BOL 中发现了这样的注释:MIN 查找基础数据库中定义的整理序列中的最低值

因此,如果我有一个表,其中一行具有以下值:

Data

AA
AB
AC

执行 SELECT MIN(DATA)会返回AA。我只是想了解其背后的原因并更好地理解 BOL。

谢谢!

I'm doing a little digging and looking for a explanation on how SQL server evaluates MIN(Varchar).

I found this remark in BOL: MIN finds the lowest value in the collating sequence defined in the underlying database

So if I have a table that has one row with the following values:

Data

AA
AB
AC

Doing a SELECT MIN(DATA) would return back AA. I just want to understand the why behind this and understand the BOL a little better.

Thanks!

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

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

发布评论

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

评论(3

眼波传意 2024-10-15 23:58:04

它由排序规则(排序顺序)决定。对于大多数文化,排序顺序与英语字母表中的字母顺序相同,因此:

  • “AA”<“ 'AB'
  • 'AA' < 'AC'
  • 'AB' < 'AC'

因此'AA' 是最小值。对于其他文化,这可能不成立。例如,丹麦语排序规则将返回“AB”作为最小值,因为“AA”>“AB”。 'AB'。这是因为“AA”被视为等同于丹麦字母表中的最后一个字母“Å”。

SELECT MIN(s COLLATE Danish_Norwegian_CI_AS) FROM table1;

min_s
AB

要获得“普通”排序顺序,请使用 Latin1_General_Bin 排序规则:

SELECT MIN(s COLLATE Latin1_General_Bin) FROM table1;

min_s
AA

要重现此结果,您可以创建此测试表:

CREATE TABLE table1 (s varchar(100));
INSERT INTO table1 (s) VALUES ('AA'), ('AB'), ('AC');

It's determined by the collation (sort order). For most cultures the collation order is the same as the alphabetical order in the English alphabet so:

  • 'AA' < 'AB'
  • 'AA' < 'AC'
  • 'AB' < 'AC'

Therefore 'AA' is the minimum value. For other cultures this may not hold. For example a Danish collation would return 'AB' as the minimum because 'AA' > 'AB'. This is because 'AA' is treated as equivalent to 'Å' which is the last letter in the Danish alphabet.

SELECT MIN(s COLLATE Danish_Norwegian_CI_AS) FROM table1;

min_s
AB

To get an "ordinary" sort order use the Latin1_General_Bin collation:

SELECT MIN(s COLLATE Latin1_General_Bin) FROM table1;

min_s
AA

To reproduce this result you can create this test table:

CREATE TABLE table1 (s varchar(100));
INSERT INTO table1 (s) VALUES ('AA'), ('AB'), ('AC');
夜血缘 2024-10-15 23:58:04

不,MIN 用于扫描多行的 SELECT 语句。它接受一列作为参数,并返回在该列中找到的“最低”值(同样,根据排序规则)。

如果不使用 GROUP BY 子句,结果集将只有一行,并且 MIN 的值将是该列中找到的最小值。与 GROUP BY 子句一起使用时,结果集将为每个组包含一行,并且 MIN 的值将是该组中任何行的该列中的最小值。

No, MIN is used in a SELECT statement that scans more than one line. It takes a column as an argument, and returns the "lowest" value (again, according to the collation sequence) found in that column.

Used without a GROUP BY clause, the result set will have a single row, and the value of MIN will be the lowest value found in that column. Used with a GROUP BY clause, the result set will have one row for each group and the value of MIN will be the lowest value in that column for any row in the group.

£噩梦荏苒 2024-10-15 23:58:04

min(x),其中 是 char(字符串)类型 -- char()、varchar()、nchar()、nvarchar(),根据 SQL 的字符串比较规则查找组中的最小值:

  • 如果两个字符串不同在长度上,较短的部分用 SP 字符(空格)填充到较长的长度。
  • 根据所使用的排序规则序列的规则,从左到右、逐个字符地进行比较。
  • 在比较中,值 NULL 比较低于任何非空值(ISO/ANSI SQL 标准表示,NULL 是否比较低于或高于任何非空值是一种实现选择)。

因此,如果您有一个表

create table foo
(
  myString varchar(16) not null ,
)

,那么运行查询

select min(myString) from foo

将为您提供与执行查询相同的结果集。

set rowcount 1

select myString
from foo
order by myString

set rowcount 0

您基本上是按升序对集合进行排序并选择第一个值。 MAX() 或当然可以提供相反的结果,按降序对集合进行排序并选择第一个值。

min(x), where is a char (string) type -- char(), varchar(), nchar(), nvarchar(), finds the lowest value in the group, based on SQL's string comparison rules:

  • if two strings differ in length, the shorter is padded with SP characters (spaces) to the length of the longer.
  • comparison proceeds left-to-right, character by character, according to the rule of the collation sequence in use.
  • in comparisons, the value NULL compares lower than any non-null values (the ISO/ANSI SQL standard says that it is an implementation choice as to whether NULL collates lower or higher than any non-null value).

So, if you have a table

create table foo
(
  myString varchar(16) not null ,
)

then running the query

select min(myString) from foo

will give you the same result set as if you executed

set rowcount 1

select myString
from foo
order by myString

set rowcount 0

You are basically ordering the set in ascending sequence and selecting the first value. MAX(), or course, gives you the inverse, ordering the set in descending sequence and selecting the first value.

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