SQL 服务器与 Postgresql 中的 ORDER BY
ORDER BY in SQL Server vs Postgresql
我正在尝试 运行 在一个巨大的数据集上进行 ORDER BY 并生成密集排名值以根据密集 rank.Later 提取不同的行数,我正在使用密集排名值作为我整个过程中的代理键来结转中间结果并将其进一步用于某些性能测试。
我面临的问题:-
我遇到的问题是我将数据集从 Matrix(postgresql)(生产环境)迁移到 SQL 服务器(新环境)并实现了密集排序功能。但是由于 SQL Server 和 Matrix 中的 ORDER BY 子句行为导致具有不同的密集排名结果,我无法进一步验证代码和结果。
测试示例:-
SELECT *,DENSE_RANK() OVER ( ORDER BY Col ) AS drnk FROM
(
SELECT '#2 Mayfair Terrace' AS Col
UNION
SELECT '#2 MYSTIC COURT' AS Col
)Z;
SQL 服务器输出:-
Col drnk
#2 Mayfair Terrace 1
#2 MYSTIC COURT 2
矩阵(Postgresql)输出:-
Col drnk
#2 MYSTIC COURT 1
#2 Mayfair Terrace 2
有人能告诉我为什么 ORDER BY 的行为在两种不同的数据库环境中存在差异吗?任何帮助,将不胜感激。谢谢
这是排序方式不同造成的collation. Try using collate
select *
, dense_rank() over (
order by col collate sql_latin1_general_cp437_bin
) as drnk
from (
select '#2 Mayfair Terrace' as col
union all
select '#2 MYSTIC COURT' as col
) Z;
returns
+--------------------+------+
| col | drnk |
+--------------------+------+
| #2 MYSTIC COURT | 1 |
| #2 Mayfair Terrace | 2 |
+--------------------+------+
rextester:http://rextester.com/QGM99129
select *
, dense_rank() over (
order by col collate
sql_latin1_general_cp437_bin
) as drnk
from (
select '#2 Mayfair Terrace' as col
union all select '#2 MYSTIC COURT' as col
union all select '#2 NAYSTIC COURT' as col
union all select '#2 NaYSTIC COURT' as col
union all select '#2 LaYSTIC COURT' as col
) Z;
returns
+--------------------+------+
| col | drnk |
+--------------------+------+
| #2 LaYSTIC COURT | 1 |
| #2 MYSTIC COURT | 2 |
| #2 Mayfair Terrace | 3 |
| #2 NAYSTIC COURT | 4 |
| #2 NaYSTIC COURT | 5 |
+--------------------+------+
我正在尝试 运行 在一个巨大的数据集上进行 ORDER BY 并生成密集排名值以根据密集 rank.Later 提取不同的行数,我正在使用密集排名值作为我整个过程中的代理键来结转中间结果并将其进一步用于某些性能测试。
我面临的问题:-
我遇到的问题是我将数据集从 Matrix(postgresql)(生产环境)迁移到 SQL 服务器(新环境)并实现了密集排序功能。但是由于 SQL Server 和 Matrix 中的 ORDER BY 子句行为导致具有不同的密集排名结果,我无法进一步验证代码和结果。
测试示例:-
SELECT *,DENSE_RANK() OVER ( ORDER BY Col ) AS drnk FROM
(
SELECT '#2 Mayfair Terrace' AS Col
UNION
SELECT '#2 MYSTIC COURT' AS Col
)Z;
SQL 服务器输出:-
Col drnk
#2 Mayfair Terrace 1
#2 MYSTIC COURT 2
矩阵(Postgresql)输出:-
Col drnk
#2 MYSTIC COURT 1
#2 Mayfair Terrace 2
有人能告诉我为什么 ORDER BY 的行为在两种不同的数据库环境中存在差异吗?任何帮助,将不胜感激。谢谢
这是排序方式不同造成的collation. Try using collate
select *
, dense_rank() over (
order by col collate sql_latin1_general_cp437_bin
) as drnk
from (
select '#2 Mayfair Terrace' as col
union all
select '#2 MYSTIC COURT' as col
) Z;
returns
+--------------------+------+
| col | drnk |
+--------------------+------+
| #2 MYSTIC COURT | 1 |
| #2 Mayfair Terrace | 2 |
+--------------------+------+
rextester:http://rextester.com/QGM99129
select *
, dense_rank() over (
order by col collate
sql_latin1_general_cp437_bin
) as drnk
from (
select '#2 Mayfair Terrace' as col
union all select '#2 MYSTIC COURT' as col
union all select '#2 NAYSTIC COURT' as col
union all select '#2 NaYSTIC COURT' as col
union all select '#2 LaYSTIC COURT' as col
) Z;
returns
+--------------------+------+
| col | drnk |
+--------------------+------+
| #2 LaYSTIC COURT | 1 |
| #2 MYSTIC COURT | 2 |
| #2 Mayfair Terrace | 3 |
| #2 NAYSTIC COURT | 4 |
| #2 NaYSTIC COURT | 5 |
+--------------------+------+