Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Oracle doesn't sort properly when using the same column twice

When I run this query, results aren't sorted correctly.

SELECT 
  SYSDATE - datecolumn a,
  SYSDATE - datecolumn sortcolumn
FROM atable
ORDER BY sortcolumn

Produces:

2576.780243055555555555555555555555555556
2586.297013888888888888888888888888888889
2342.294479166666666666666666666666666667
2617.297476851851851851851851851851851852
2624.855104166666666666666666666666666667
2624.855138888888888888888888888888888889
2624.854236111111111111111111111111111111
2372.296643518518518518518518518518518519
2645.257800925925925925925925925925925926
2403.294756944444444444444444444444444444
2676.297696759259259259259259259259259259

When the first line (SYSDATE - datecolumn a,) is removed, everything works fine. Is there any particular reason why this is happening?

like image 938
Adam Brokes Avatar asked May 13 '14 15:05

Adam Brokes


People also ask

Does group by sort data?

In this SQL tutorial, we will learn how to use Order by and Group By in SQL. Group By in SQL is used to arrange similar data into groups and Order By in SQL is used to sort the data in ascending or descending order.

How do I sort columns in SQL Developer?

Just right-click on one of the column headings in the Data tab, select Columns..., then use the Manage Column dialog's Up/Down arrows to order the columns as desired.

What is sorting in Oracle database?

In Oracle, ORDER BY Clause is used to sort or re-arrange the records in the result set. The ORDER BY clause is only used with SELECT statement.


1 Answers

This appears to be a bug. This behaviour looks similar to bug 8675087, which is supposedly fixed in 11.2.0.2 - and indeed I can't reproduce the test case for that. But I still see your behaviour in that version (on SQL Fiddle) and in 11.2.0.3, so this is similar but not exactly the same...

If this is causing you a real issue and you can't find a workaround you're happy with (like swapping the column order; if your sortcolumn is the first expression that uses sysdate it seems to be OK, but swapping columns in your real query might not be convenient) then you should raise a service request with Oracle.

like image 130
Alex Poole Avatar answered Oct 21 '22 10:10

Alex Poole