【问题标题】:Why do I get different sort order of the same query executed in Teradata & Snowflake?为什么在 Teradata 和 Snowflake 中执行的相同查询的排序顺序不同?
【发布时间】:2021-03-18 21:00:01
【问题描述】:

即使我在两个查询中都添加了 order by 子句,我也会得到不同的排序顺序。 以下是我在 Teradata 和雪花上执行的查询-

Teradata:

Select column1, column2, column3, column4,column5 From Teradata_Table  Where column1 in (1027837724,1040965189,1046224751) and column2='2021-03-11'
order by column1 ; 

结果 -->

column1 column2 column3 column4 column5
1027837724 3/11/2021 0 42.66 5942
1027837724 3/11/2021 12052.34 2.99 5735
1040965189 3/11/2021 5336.72 10.87 5944
1040965189 3/11/2021 0 311.16 5942
1046224751 3/11/2021 0 14.99 5735
1046224751 3/11/2021 0 2.99 5735

雪花:

Select column1, column2, column3, column4,column5 From Snowflake_Tablename  Where column1 in (1027837724,1040965189,1046224751) and column2='2021-03-11'
order by column 1

结果 -->

column1 column2 column3 column4 column5
1027837724 3/11/2021 12052.34 2.99 5735
1027837724 3/11/2021 0 42.66 5942
1040965189 3/11/2021 5336.72 10.87 5944
1040965189 3/11/2021 0 311.16 5942
1046224751 3/11/2021 0 14.99 5735
1046224751 3/11/2021 0 2.99 5735

【问题讨论】:

  • 您仅按列指定单个订单。您在该列中有多个具有相同值的行,因此其中的排序基本上是随机的。
  • 是的。如果您在同一个 DBMS 上运行两次查询,甚至不能保证对具有相同 ORDER BY 列的一组行进行排序

标签: sorting sql-order-by teradata snowflake-cloud-data-platform resultset


【解决方案1】:

正如 cmets 所说:

  • 您仅按一列排序,并且无论数据库如何,按该列排序的行的顺序都是相同的。
  • 但您并没有要求数据库按任何其他列排序,因此具有相同 column1 的行可以以任意顺序显示。

如果你想让顺序一致,你应该ORDER BY column1, column2, column3, etc

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-09-09
    • 2018-06-16
    • 2021-03-10
    • 2013-08-07
    • 2017-01-12
    • 2013-02-08
    • 2021-02-26
    相关资源
    最近更新 更多