View Single Post

   
  #2 (permalink)  
Old 03-01-2008, 03:49 PM
Jerry Stuckle
 
Posts: n/a
Default Re: SELECT, GROUP BY & HAVING problem!

stockton wrote:
> This is my TABLE
>
> ID_1 ID_2 DAY MONTH
> -----------------------------------------------
> 1 1 MON JAN
> 2 1 MON JAN
> 3 2 TUE FEB
> 4 2 TUE MAR
>
> This is my SELECT STATEMENT
>
> SELECT ID_2, DAY, COUNT(*) AS COUNT FROM `TABLE` GROUP BY `ID_2`,
> `DAY` HAVING COUNT > 1
>
> This RETURNS
>
> ID_2 DAY COUNT
> ----------------------------------
> 1 MON 2
> 2 TUE 2
>
> However, what I want to do is only return when there are multiple rows
> (more than one) with ID_2 and DAY that match but where the MONTH is
> different i.e. RETURN
>
> ID_2 DAY COUNT
> -----------------------------------
> 2 TUE 2
>
> Any ideas?
>
> Thanks in advance
>


Not tested:

SELECT ID_2, DAY, COUNT(*) AS COUNT, DISTINCT(MONTH) FROM `TABLE` GROUP
BY `ID_2`, `DAY` HAVING COUNT > 1


--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

Reply With Quote