0

I noticed that in PostgreSQL the following two queries output different results:

select a.*
from (
    select distinct on (t1.col1)
        t1.*
    from t1
    order by t1.col1, t1.col2
) a
where a.col3 = value
;
create table temp as
select distinct on (t1.col1)
    t1.*
from t1
order by t1.col1, t1.col2
;
select temp.*
from temp
where temp.col3 = value
;

I guess it has something to do with using distinct on in subqueries.

What is the correct way to use distinct on in subqueries? E.g. can I use it if I don't use where statement? Or in queries like

(
select distinct on (a.col1)
    a.*
from a
)
union
(
select distinct on (b.col1)
    b.*
from b
)
2
  • 2
    Please provide a minimum reproducible example: sample data and desired results, as tabular text. Commented Sep 11, 2020 at 8:37
  • IMHO both should return the same result. Commented Sep 11, 2020 at 8:39

1 Answer 1

1

In normal situation, both examples should return the same result.

I suspect that you are getting different results because the order by clause of your distinct on subquery is not deterministic. That is, there may be several rows in t1 sharing the same col1 and col2.

If the columns in the order by do not uniquely identify each row, then the database has to make its own decision about which row will be retained in the resultset: as a consequence, the results are not stable, meaning that consecutive executions of the same query may yield different results.

Make sure that your order by clause is deterministic (for example by adding more columns in the clause), and this problem should not arise anymore.

Sign up to request clarification or add additional context in comments.

1 Comment

Then running query 1 several times should also result in different results: ( select distinct on (t1.col1) t1.* from t1 order by t1.col1, t1.col2 ) except ( select distinct on (t1.col1) t1.* from t1 order by t1.col1, t1.col2 )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.