From: | "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com> |
---|---|
To: | Guyren Howe <guyren(at)gmail(dot)com> |
Cc: | pgsql-general <pgsql-general(at)postgresql(dot)org> |
Subject: | Re: SELECT DISTINCT ON removes results |
Date: | 2016-10-28 20:50:52 |
Message-ID: | CAKFQuwZP0nFa1BuaZYD64V3kvXJET_7D6kwtJmdW8QR-p5uuxg@mail.gmail.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-general |
On Fri, Oct 28, 2016 at 1:39 PM, Guyren Howe <guyren(at)gmail(dot)com> wrote:
> Using 9.5, this query:
>
> SELECT o.id,
> a.number AS awb
> FROM pt.orders o
> LEFT JOIN (
> SELECT DISTINCT ON ((string_agg(air_way_bills.number::text, ','::text)))
> string_agg(air_way_bills.number::text, ','::text) AS number,
> air_way_bills.order_id
> FROM pt.air_way_bills
> GROUP BY air_way_bills.order_id) a ON a.order_id = o.id
>
> gives me null for awb. Removing the DISTINCT ON clause:
>
> SELECT o.id,
> a.number AS awb
> FROM pt.orders o
> LEFT JOIN (
> SELECT string_agg(air_way_bills.number::text, ','::text) AS number,
> air_way_bills.order_id
> FROM pt.air_way_bills
> GROUP BY air_way_bills.order_id) a ON a.order_id = o.id
> where o.id = 2792;
>
> gives me an awb. I'm confused about how this can be.
>
On its face the statement "DISTINCT ON removes results" is not at all
surprising given its definition.
Given a self-contained query exhibiting the desired behavior I might be
willing to figure out and explain exactly why its happening in that
particular circumstance.
Oh, and on its face your DISTINCT ON query doesn't make any sense to me.
Using DISTINCT ON on one column but then joining on the discard-able ID
column is...unusual.
And it also lacks an ORDER BY for deterministic discarding of duplicate
rows.
David J.
From | Date | Subject | |
---|---|---|---|
Next Message | Guyren Howe | 2016-10-28 21:15:23 | Re: SELECT DISTINCT ON removes results |
Previous Message | Guyren Howe | 2016-10-28 20:39:19 | SELECT DISTINCT ON removes results |