Oracle FAQ | Your Portal to the Oracle Knowledge Grid |
Home -> Community -> Usenet -> c.d.o.server -> Re: sql help
"Daniel Morgan" <dmorgan_at_exesolutions.com> wrote in message
news:3CD8381D.382F11E3_at_exesolutions.com...
> Brian wrote:
>
> > Hello all,
> >
> > I'm having difficulties completing this query. I have a table with
a
> > primary key of ID1 and a foriegn key of ID2. What I want my qeury
to
> > do is return one primary key (ID1) for each foreign key. Example:
> > From the table below -
> >
> > ID1 ID2
> > ---------- ----------
> > 4198 5511601
> > 31380 5511601
> > 1454716 5511601
> > 1455577 5511601
> > 1455637 5511601
> > 1456244 5511601
> > 1489157 5511601
> > 1503752 5511601
> > 15068 23544883
> > 42821 23544883
> > 1390131 23544883
> >
> > ID1 ID2
> > ---------- ----------
> > 1418794 23544883
> >
> > Query with a result of -
> >
> > ID1 ID2
> > ---------- ----------
> > 4198 5511601
> > 15068 23544883
> >
> > I've tried using group by and can't figure out what I need to do.
> > Any help would be great!!
> >
> > TIA
> >
> > Brian
>
> There are a number of ways this could be accomplished. But SQL, and
I,
> have a difficult time figuring out how to know that you want 4198
with
> 5511601 rather than any other value. Is your criterion ... "give me
one
> and I don't care which one"?
Based on the example, it seems that a 'min' is what is wanted:
SQL> select * from fiz;
ID1 ID2
---------- ----------
4198 5511601 31380 5511601 10568 23544883 42821 23544883
SQL> select min(id1) as id1, id2 from fiz group by id2;
ID1 ID2
---------- ----------
4198 5511601 10568 23544883
No?
-- Pablo Sanchez, High-Performance Database Engineering mailto:pablo_at_hpdbe.com http://www.hpdbe.com Available for short-term and long-term contractsReceived on Tue May 07 2002 - 18:06:43 CDT