Oracle FAQ | Your Portal to the Oracle Knowledge Grid |
![]() |
![]() |
Home -> Community -> Usenet -> c.d.o.misc -> Re: Simple SQL - GROUP BY
Filip Hanik wrote:
>
> Hi,
>
> I have, ever since I learned SQL, been kind of confused by the GROUP BY
> clause.
> I have successfully used it for computing values.
>
> But here's my problem,
> I want to retrieve two columns out of a table PERSON_ID, COMPANY_ID
> but I'm only interested in retrieving on person per company.
>
> TABLE tblEmployees (
> PERSON_ID INTEGER NOT NULL,
> COMPANY_ID INTEGER NOT NULL
> )
>
> So I would like to group by COMPANY_ID but the compiler doesn't let me
> retrieve the PERSON_ID.
>
> If I put a MAX around PERSON_ID will I get a correct person_id like this?
> SELECT MAX(PERSON_ID), COMPANY_ID FROM tblEmployees GROUP BY COMPANY_ID;
>
> thanks
>
> Filip
> fhanik_at_digitalworkforce.net
If the PERSON_ID is not 'paired' with COMPANY_ID, how do you know which PERSON_ID to list with each COMPANY_ID?
If you can accept the 'max' person for each company, then you might be able to do something like this:
select max(person_id),company_id from tblEmployees group by company_id;
Yours,
Geoff Houck
systems hk
hksys_at_teleport.com
http://www.teleport.com/~hksys
Received on Fri Jul 02 1999 - 02:08:50 CDT
![]() |
![]() |