Subject Re: From where Firebird get field values in ORDER BY if DISTINCT is applied
Author Dmitry Yemanov
13.08.2015 11:16, liviuslivius wrote:
>
> query like this
> SELECT DISTINCT T.FIELD_1 FROM TEST T ORDER By T.FIELD_2, T.FIELD_1
> from where Firebird get info about T.FIELD_2 in order by if it applied
> first DISTINCT (unique sort)?
> i run this also on FB3 to get more info from explained plan
> Select Expression
> -> Sort (record length: 32, key length: 16)
> -> Unique Sort (record length: 32, key length: 8)
> -> Table "TEST " as "T" Full Scan
> Is this a bug? Because first FB sort unique results and got only unique
> values of FIELD_1 and it can not know what value is in FIELD_2 after
> unique sort.

Interesting question. FIELD_2 is preserved during the unique sort, so FB
knows it while performing the outer sort. The problem is what values of
FIELD_2 will remain after the unique sort. I believe the answer is
"random/unknown", e.g. the ones matching the first unique FIELD_1 being
processed in each group.

Maybe this counts as a real bug, because I suppose you cannot do:

SELECT FIELD_1
FROM TEST
GROUP BY FIELD_1
ORDER BY FIELD_2, FIELD_1


Dmitry