[ 
https://issues.apache.org/jira/browse/DERBY-2351?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#action_12473871
 ] 

Yip Ng commented on DERBY-2351:
-------------------------------

Ah, I think I see why this particular case should throw an exception.  The 
result is not predictable (and not portable) if the ORDER BY sort key is not in 
the SELECT list.  Consider the following example:

create table person (name varchar(10), age int);
insert into person values ('John', 10);
insert into person values ('John', 30);
insert into person values ('Mary', 20);

SELECT DISTINCT name FROM person ORDER BY age;

So which row to display for John?  The John (age 10) or the other John (age 
30)?  
Result may be:
John
Mary

or

Mary
John

Derby should not allow this and return an error.


> ORDER BY with expression with distinct in the select list returns incorrect 
> result
> ----------------------------------------------------------------------------------
>
>                 Key: DERBY-2351
>                 URL: https://issues.apache.org/jira/browse/DERBY-2351
>             Project: Derby
>          Issue Type: Bug
>          Components: SQL
>    Affects Versions: 10.2.2.0, 10.3.0.0
>         Environment: Any
>            Reporter: Yip Ng
>
> When distinct is in the select list and the query has order by with 
> expression, the resultset produced contains an additional column.  
> ij> create table t1 (c1 int, c2 varchar(10))
> 0 rows inserted/updated/deleted
> ij> insert into t1 values (1,'a'),(2,'b'),(3,'c');
> 3 rows inserted/updated/deleted
> select distinct c1, c2 from t1 order by c1;
> C1         |C2
> ----------------------
> 1          |a
> 2          |b
> 3          |c
> 3 rows selected
> ij> select distinct c1, c2 from t1 order by c1+1;
> C1         |C2        |3                                 <=====returns 3 
> columns, incorrect result returned
> ----------------------------------
> 1          |a         |2
> 2          |b         |3
> 3          |c         |4
> 3 rows selected

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.

Reply via email to