May 3, 2006

"SELECT DISTINCT ... ORDER BY" is broken in Sybase ASE 12.5.3

So, last night I was running into broken behavior in Sybase 12.5.3 with regards to its behavior with SELECT COUNT(DISTINCT columnname) ... which surprised me, but I could at least rationalize why it was happening. Today, I found an outright bug, where mixing SELECT DISTINCT ... with an ORDER BY clause gives the wrong behavior. Here's a quick session that demonstrates what I'm talking about:

1> SELECT @@version; -m bcp
Adaptive Server Enterprise/12.5.3/EBF 12331 ESD#1/P/Sun_svr4/OS 5.8/ase1253/1900/64-bit/FBO/Tue Jan 25 08:52:58 2005|

1> CREATE TABLE #test (x int, y int);

1> INSERT INTO #test (x, y) VALUES (1, 2);
(1 row affected)
1> INSERT INTO #test (x, y) VALUES (1, 10);  
(1 row affected)
1> INSERT INTO #test (x, y) VALUES (2, 1);   
(1 row affected)
1> INSERT INTO #test (x, y) VALUES (2, 5);   
(1 row affected)
1> INSERT INTO #test (x, y) VALUES (3, 4);  
(1 row affected)
1> INSERT INTO #test (x, y) VALUES (3, 7); 
(1 row affected)

1> SELECT DISTINCT x FROM #test ORDER BY y DESC;
 x          
 -----------
           1
           3
           2
           3
           1
           2

Has this been fixed in a newer version of Sybase? I can't imagine anyone actually thinks this the right behavior.

Posted by dossy at 09:25 AM | 276 | Link | Comments (4) | Trackbacks (0) | Bloglines | Feedster | Technorati | BlogPulse
Fashion Bug


Comments
1
Gravatar Icon

I tried reproducing this on SQL Server 2000 and SQL Server won't even allow you to do that type of DISTINCT/ORDER BY mix. I got the following error:

ORDER BY items must appear in the select list if SELECT DISTINCT is specified.

Clearly, there is something with this type of mix that Sybase didn't handle properly if Microsoft disabled ordering by columns that did not appear in a distinct resultset.

Posted by: sgoguen on May 3, 2006 at 12:33 PM | Permalink
2
Gravatar Icon

SQL can be ambiguous, and this is one of those situations. PostgreSQL mandates an ORDER BY or GROUP BY in DISTINCT-styled queries.


havard=# SELECT distinct a from test order by b desc;
ERROR: for SELECT DISTINCT, ORDER BY expressions must appear in select list
jhavard=# SELECT distinct on (a) a from test order by b desc;
ERROR: SELECT DISTINCT ON expressions must match initial ORDER BY expressions

Take a look at the query optimizer and query planner output if possible. I'm willing to bet the query is executed as if it were SELECT DISTINCT ON (b) a FROM test ORDER BY b. After all, you are supplying an ordered field.

Posted by: jh on May 3, 2006 at 01:05 PM | Permalink
3
Gravatar Icon

Steve:

Interestingly, MySQL 4.1.11 handles this "correctly" as I would have expected:

mysql> SELECT DISTINCT x FROM test ORDER BY y DESC;
+------+
| x |
+------+
| 3 |
| 1 |
| 2 |
+------+
3 rows in set (0.00 sec)

Posted by: Dossy on May 3, 2006 at 02:07 PM | Permalink
4
Gravatar Icon

The ASE Transact-SQL User's Guide addresses this on page 105 of the 12.5.1 edition.

"If a query has an 'order by' or 'group by' clause that includes columns not in the select list, Adaptive Server adds those columns as hidden columns in the columns being processed. The columns listed in the 'order by' or 'group by' clause are included in the test for distinct rows. To comply with ANSI standards, include the 'order by' or 'group by' column in the select list."

Posted by: RoyceB on May 23, 2006 at 07:23 PM | Permalink
Post a comment

Who are you?

(what is this?)
(what is this?)
(what is this?)
(what is this?)


(required)
(required, not displayed)


Allowed markup: a href title, abbr title, acronym title, b, blockquote cite type, br, code, dd, dl, dt, em, i, li, ol, p, pre, strike, strong, tt, ul.

Please type in the letters shown in the image below:
Graphical CAPTCHA.

A preview of your comment:

Posted by: