Depends.  If this is a correlation table and the combined columns are a
primary key,

SELECT ObjId,
               COUNT(ElemId)
FROM    Elems
GROUP BY ElemId;

if the combination is not unique, then:

SELECT ObjId,
               COUNT(DISTINCT ElemId)
FROM    Elems
GROUP BY ElemId;

----- Original Message -----
From: "Csaba Gabor" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Friday, June 07, 2002 10:20 AM
Subject: Counting distinct elements


> Seems like this should be an easy question, but I just don't get it:
>
> If I have CREATE TABLE Elems (ObjId INT, ElemId INT);
> Is there a nice query to find the Number of Distinct ElemId's for each
ObjId?
>
> Motivation: Later I will want to add a WHERE ObjId IN (...) and join the
Obj describing table to
> the results of this query.
>
> Thanks,
>     Csaba Gabor from New York
>
> __________________________________________________
> Do You Yahoo!?
> Yahoo! - Official partner of 2002 FIFA World Cup
> http://fifaworldcup.yahoo.com
>
> ---------------------------------------------------------------------
> Before posting, please check:
>    http://www.mysql.com/manual.php   (the manual)
>    http://lists.mysql.com/           (the list archive)
>
> To request this thread, e-mail <[EMAIL PROTECTED]>
> To unsubscribe, e-mail
<[EMAIL PROTECTED]>
> Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php
>


---------------------------------------------------------------------
Before posting, please check:
   http://www.mysql.com/manual.php   (the manual)
   http://lists.mysql.com/           (the list archive)

To request this thread, e-mail <[EMAIL PROTECTED]>
To unsubscribe, e-mail <[EMAIL PROTECTED]>
Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php

Reply via email to