Unix Technical Forum

SELECT, GROUP BY & HAVING problem!

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


Go Back   Unix Technical Forum > Database Server Software > MySQL

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 03-01-2008, 02:49 PM
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 03-01-2008, 02: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
==================

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 03-01-2008, 02:49 PM
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?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 03-01-2008, 02:49 PM
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?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 03-01-2008, 02:49 PM
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*.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 03-01-2008, 02:49 PM
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
==================

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 03-01-2008, 02:49 PM
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 03-01-2008, 02:49 PM
stockton
 
Posts: n/a
Default Re: SELECT, GROUP BY & HAVING problem!

thanks
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 03:06 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com