SQL SERVER - 了解 MIN(text) 的工作原理
我正在做一些挖掘并寻找有关 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
它由排序规则(排序顺序)决定。对于大多数文化,排序顺序与英语字母表中的字母顺序相同,因此:
因此'AA' 是最小值。对于其他文化,这可能不成立。例如,丹麦语排序规则将返回“AB”作为最小值,因为“AA”>“AB”。 'AB'。这是因为“AA”被视为等同于丹麦字母表中的最后一个字母“Å”。
要获得“普通”排序顺序,请使用
Latin1_General_Bin
排序规则:要重现此结果,您可以创建此测试表:
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:
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.
To get an "ordinary" sort order use the
Latin1_General_Bin
collation:To reproduce this result you can create this test table:
不,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.
min(x),其中 是 char(字符串)类型 -- char()、varchar()、nchar()、nvarchar(),根据 SQL 的字符串比较规则查找组中的最小值:
因此,如果您有一个表
,那么运行查询
将为您提供与执行查询相同的结果集。
您基本上是按升序对集合进行排序并选择第一个值。 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:
So, if you have a table
then running the query
will give you the same result set as if you executed
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.