List:General Discussion« Previous MessageNext Message »
From:Nicolas Prade Date:March 2 2000 3:58pm
Subject:RE: COUNTing
View as plain text  

> -----Original Message-----
> From: Graeme B. Davis [mailto:GraemeD@stripped]
> Sent: jeudi 2 mars 2000 16:19
> To: Jonathan Stimmel; mysql@stripped; ant@stripped
> Subject: Re: COUNTing
> 
> 
> Thanks for the info!
> 
> Is there a way to return this information in the same row
> 
> Right now it returns info like this:
> 
> | son          | Email  |       12 |
> | son          | Phone  |        2 |
> 
> But I would want it to return like:
> 
> | son          | 12 | 2 |
> 
> Any ideas?
> 
> > What you're really doing is looking to count groups of "person"
> > and "medium". Given that, how about something like:
> > 
> >  SELECT person, medium, COUNT(*)
> >  FROM table
> >  WHERE medium IN ('Phone', 'Email)
> >  GROUP BY person, medium;
> > 

Hello,
you can try:

SELECT person,
SUM(IF(medium='Phone',1,0)) AS s1,
SUM(IF(medium='Email',1,0)) AS s2
FROM table
GROUP BY person HAVING s1+s2>0;

I would not call this "optimized" though.
Hope this helps,
Nicolas.

Thread
COUNTingGraeme B. Davis2 Mar
  • Re: COUNTingJonathan Stimmel2 Mar
  • Re: COUNTingGraeme B. Davis2 Mar
    • RE: COUNTingNicolas Prade2 Mar