Re: Selct DISTINCT

From: David Portas <REMOVE_BEFORE_REPLYING_dportas_at_acm.org>
Date: Tue, 29 Jan 2008 14:43:53 -0800 (PST)
Message-ID: <743c3678-dab1-4cb3-8320-b7a9c095866f@j78g2000hsd.googlegroups.com>


On 29 Jan, 20:29, trp..._at_gmail.com wrote:
> Let me expand a little on what I am actually doing, I provided a small
> example in the original post as I thought there might be a simple way
> to select the first distinct row based on one column.
>
> Without specifics, here is how my query is structured:
>
> SELECT DISTINCT(ID), NAME, SSN FROM
> (
>
>      SELECT DISTINCT(ID), NAME, SSN FROM TABLE1
>
>      UNION ALL
>
>      SELECT DISTINCT(ID), NAME, SSN FROM TABLE2
>
> )
>
> So the problem I have is that there is some duplicate data in TABLE1
> and TABLE2, but TABLE1 data always has more data available then in
> TABLE2, so that is how I know if there is a duplicate to take the
> first row.

Maybe this is what you want:

SELECT id, name, ssn FROM Table1
UNION
SELECT id, name, ssn FROM Table2
WHERE NOT EXISTS
(SELECT id FROM Table1 WHERE id = Table2.id);

--
David Portas
Received on Tue Jan 29 2008 - 16:43:53 CST

Original text of this message