SELECT, GROUP BY & HAVING problem!

This is a discussion on SELECT, GROUP BY & HAVING problem! within the MySQL Database forums, part of the Database Forums category; This is my TABLE ID_1 ID_2 DAY MONTH ----------------------------------------------- 1 1 MON JAN 2 1 MON JAN 3 2 TUE FEB ...


Go Back   Usenet Forums > Database Forums > MySQL Database

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-28-2008
stockton
 
Posts: n/a
Default SELECT, GROUP BY & HAVING problem!

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
Reply With Quote
  #2 (permalink)  
Old 02-29-2008
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
  #3 (permalink)  
Old 02-29-2008
stockton
 
Posts: n/a
Default Re: SELECT, GROUP BY & HAVING problem!

> Not tested:

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



Unfortunately this doesn't work!

Any other ideas?
Reply With Quote
  #4 (permalink)  
Old 02-29-2008
Captain Paralytic
 
Posts: n/a
Default Re: SELECT, GROUP BY & HAVING problem!

On 29 Feb, 06:36, stockton <simon.stock...@baesystems.com> wrote:
> > Not tested:
> > SELECT ID_2, DAY, COUNT(*) AS COUNT, DISTINCT(MONTH) FROM `TABLE` GROUP
> > BY `ID_2`, `DAY` HAVING COUNT > 1

>
> Unfortunately this doesn't work!
>
> Any other ideas?


You don't deserve more help when you post things like "this doesn't
work" without explaining precisely what doesn't work with it. But I'm
feeling generous today.

Do you actually need the count, or was it only there to detect the
multiple rows?
Reply With Quote
  #5 (permalink)  
Old 02-29-2008
Pavel Lepin
 
Posts: n/a
Default Re: SELECT, GROUP BY & HAVING problem!


stockton <simon.stockton@baesystems.com> wrote in
<cbc1feba-429e-435f-a589-4654ffc0771b@u69g2000hse.googlegroups.com>:
>> SELECT ID_2, DAY, COUNT(*) AS COUNT, DISTINCT(MONTH) FROM
>> `TABLE` GROUP BY `ID_2`, `DAY` HAVING COUNT > 1

>
> Unfortunately this doesn't work!


Jerry was probably just a bit low on caffeine or something:

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

But really, you should've been able to figure that out
yourself. That's what the docs are for.

--
In Soviet Russia, XML documents transform *you*.
Reply With Quote
  #6 (permalink)  
Old 02-29-2008
Jerry Stuckle
 
Posts: n/a
Default Re: SELECT, GROUP BY & HAVING problem!

Pavel Lepin wrote:
> stockton <simon.stockton@baesystems.com> wrote in
> <cbc1feba-429e-435f-a589-4654ffc0771b@u69g2000hse.googlegroups.com>:
>>> SELECT ID_2, DAY, COUNT(*) AS COUNT, DISTINCT(MONTH) FROM
>>> `TABLE` GROUP BY `ID_2`, `DAY` HAVING COUNT > 1

>> Unfortunately this doesn't work!

>
> Jerry was probably just a bit low on caffeine or something:
>
> SELECT ID_2, DAY, COUNT(DISTINCT MONTH) AS COUNT FROM
> `TABLE` GROUP BY `ID_2`, `DAY` HAVING COUNT > 1;
>
> But really, you should've been able to figure that out
> yourself. That's what the docs are for.
>


No, wrong time of the day. Shouldn't do it shortly before bedtime :-)

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

Reply With Quote
  #7 (permalink)  
Old 02-29-2008
Captain Paralytic
 
Posts: n/a
Default Re: SELECT, GROUP BY & HAVING problem!

On 29 Feb, 11:06, Pavel Lepin <p.le...@ctncorp.com> wrote:
> stockton <simon.stock...@baesystems.com> wrote in
> <cbc1feba-429e-435f-a589-4654ffc07...@u69g2000hse.googlegroups.com>:
>
> >> SELECT ID_2, DAY, COUNT(*) AS COUNT, DISTINCT(MONTH) FROM
> >> `TABLE` GROUP BY `ID_2`, `DAY` HAVING COUNT > 1

>
> > Unfortunately this doesn't work!

>
> Jerry was probably just a bit low on caffeine or something:
>
> SELECT ID_2, DAY, COUNT(DISTINCT MONTH) AS COUNT FROM
> `TABLE` GROUP BY `ID_2`, `DAY` HAVING COUNT > 1;
>
> But really, you should've been able to figure that out
> yourself. That's what the docs are for.
>
> --
> In Soviet Russia, XML documents transform *you*.


Here's a JOIN version for use if the count isn't actually required:
SELECT DISTINCT
i1.id_2,
i1.day
FROM table i1
JOIN table i2 ON i1.id_2 = i2.id_2 AND i1.day = i2.day AND i1.month <>
i2.month
Reply With Quote
  #8 (permalink)  
Old 03-01-2008
stockton
 
Posts: n/a
Default Re: SELECT, GROUP BY & HAVING problem!

thanks
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 03:45 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0