在返回一定数量的记录后,Order By Upper不能正确排序

时间:2014-08-27 21:11:13

标签: sql oracle

下面的两个查询完全相同,除了“Not In”条件。

以下查询会对以下名称进行正确排序:

查询:

Select (Bp.Last_Name||', '||Bp.First_Name||' '||Substr(Bp.Middle_Name,1,1)) As Username
From Scrty.User_Account Ua 
Inner Join Buspty.Business_Person Bp On Ua.Business_Person_Uuid = Bp.Business_Party_Uuid And Ua.Active_Flag = 'Y' And Bp.Last_Name Not In (**'a2s','Abdallah','Abnoosi','Abrahamson','Abrams'**) And Ua.Type = 'APPLICATION'
Inner Join Scrty.User_Account_Aaa Uaa On Ua.Uuid = Uaa.User_Account_Uuid 
Left Outer Join Buspty.Business_Organization Bo On Bo.Business_Party_Uuid = Bp.Primary_Department_Uuid 
Left Outer Join Buspty.Business_Org_Type_Domain Botd On Bo.Business_Org_Type_Uuid = Botd.Uuid And  Botd.Code =2 Left Outer Join  Buspty.Network N On  Bp.Primary_Network_Uuid = N.Uuid 
Order By Upper(Username)

结果:

TAYLOR, BRANDON
Taylor, Brandon

但是,当我在“不在”条件中排除另外一个名字时,查询会导致小写brandon taylor出现。

查询:

Select (Bp.Last_Name||', '||Bp.First_Name||' '||Substr(Bp.Middle_Name,1,1)) As Username
From Scrty.User_Account Ua 
Inner Join Buspty.Business_Person Bp On Ua.Business_Person_Uuid = Bp.Business_Party_Uuid And Ua.Active_Flag = 'Y' And Bp.Last_Name Not In (**'a2s','Abdallah','Abnoosi','Abrahamson','Abrams','Abruzzo'**) And Ua.Type = 'APPLICATION' 
Inner Join Scrty.User_Account_Aaa Uaa On Ua.Uuid = Uaa.User_Account_Uuid 
Left Outer Join Buspty.Business_Organization Bo On Bo.Business_Party_Uuid = Bp.Primary_Department_Uuid 
Left Outer Join Buspty.Business_Org_Type_Domain Botd On Bo.Business_Org_Type_Uuid = Botd.Uuid And  Botd.Code =2 Left Outer Join  Buspty.Network N On  Bp.Primary_Network_Uuid = N.Uuid 
Order By Upper(Username)

结果:

Taylor, Brandon
TAYLOR, BRANDON

1 个答案:

答案 0 :(得分:2)

您按相同的值排序,因此数据库可以自由选择任何所需的订单。这基本上好像你根本没有order by(这实际上是一个关于数据库如何根据查询中的条件更改排序顺序的一个很好的例子 - 另一个证明你永远不应该依赖查询中的任何隐式顺序结果)

要获得一致的排序顺序,您需要在order by子句中包含一些唯一的列:

Order By Upper(Username), Ua.Uuid  -- or whatever makes the combination unique
如果两个用户名(大写)相同,

将保证相同的排序顺序。您还可以包含时间戳值或其他内容。