IBM Support

Why is my report/query being returned in a different order?

News


Abstract

This TECHDOC will explain why the order of a report / query can change unless an ORDER BY is part of the query.

Content

When you run a query without an ORDER BY clause, the system can return it in any order and there is no order guaranteed.  The system happened to implement the query that returned the order you wanted but with data changes, PTFs or a new release, the system can choose a different implementation.  The order is not guaranteed unless you have an ORDER BY.  You can not force the system to use the implementation you want.  You must add the ORDER BY to ensure the data is returned in the order you want.

Documentation in our Information Center:

https://www.ibm.com/support/knowledgecenter/ssw_ibm_i_73/sqlp/rbafyorderby.htm                                                                  

The ORDER BY clause specifies the particular order in which you want    
selected rows returned. The order is sorted by ascending or descending  
collating sequence of a column's or an expression's value.              

When you do not specify an ORDER BY clause, no order is guaranteed.     

From the Query/400 manual (pg 105 chapter 8 ):                                              
https://www.ibm.com/support/knowledgecenter/ssw_i5_54/books/sc415210.pdf
                                                      
You can let Query retrieve the data directly from the database and      
include it in your query without any sorting. If the sequence of the    
data in your report is not important, you do not need to worry about    
selecting sort fields. When running a query that does not have a sort   
field specified, the order in which rows are retrieved is not           
guaranteed. If the order is important, you should define a sort field in
the query.                                                              

From TECHDOC https://www-01.ibm.com/support/docview.wss?uid=nas8N1016738                                                  
The specification states that When ordering of a cursor is not defined  
by an ORDER BY clause, the relative position of two rows is             
implementation-dependent.                                               

Implementation dependent means that the order can change due to how the
query implements.  Depending on the file, memory available to the job,  
indexes, and tons of other factors it will implement the quickest way to
get the data, this can cause the order to change. If the order is       
important, you have to specify an ORDER BY statement.                   

 

[{"Business Unit":{"code":"BU058","label":"IBM Infrastructure w\/TPS"},"Product":{"code":"SWG60","label":"IBM i"},"Component":"","Platform":[{"code":"PF012","label":"IBM i"}],"Version":"All Versions","Edition":"","Line of Business":{"code":"LOB57","label":"Power"}}]

Document Information

Modified date:
18 December 2019

UID

ibm10732890