Unix Technical Forum

group by problem

This is a discussion on group by problem within the MySQL forums, part of the Database Server Software category; --> On Mar 20, 8:28 pm, "Bob Bedford" <b...@bedford.com> wrote: > add this to the script: > INSERT INTO `folderstatus` ...


Go Back   Unix Technical Forum > Database Server Software > MySQL

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #11 (permalink)  
Old 02-28-2008, 10:26 AM
strawberry
 
Posts: n/a
Default Re: group by problem

On Mar 20, 8:28 pm, "Bob Bedford" <b...@bedford.com> wrote:
> add this to the script:
> INSERT INTO `folderstatus` VALUES ('14', '3', '5');
> update `typestatus` set idcheck = 0 where idtypestatus = 5;
>
> Now the query:
> select folder.idperson, folderstatus.idtypestatus,typestatus.idcheck,
> folderstatus.idfolderstatus,
> folder.datefolder, folder.idfolder
> from typestatus
> inner join folderstatus on typestatus.idtypestatus =
> folderstatus.idtypestatus
> inner join folder on folderstatus.idfolder = folder.idfolder
> inner join person on folder.idperson = person.idperson
> inner join client on folder.idclient = client.idclient
> where typestatus.idcheck = 0
> group by idtypestatus, idfolder
> having (idfolderstatus = max(folderstatus.idfolderstatus))
>
> I've 4 results but IdFolder 3 is shown twice and I only want the bigger
> idfolderstatus for every folder.
>
> How to do so ? I seem close to the final result, I've tried to group by
> different manners but no way. Something's wrong and I'm not a MySQL gourou,
> so please help me.
>
> Bob

Well, you must be using a different version of google! I get 66 hits
in this ng!

Anyway, here's an example of what I'm talking about. Incidentally,
this is an example of a groupwise max problem.
You may find it helpful to include fs2.* in the SELECT part of the
query in order to better understand what's going on.
I've tried to use your naming convention but I probably screwed up
somewhere. The GROUP BY part of this statement is a bit of a
nonsense. It just says 'In the event of a tie, pick one. I don't care
which.' In reality you'd probably want to handle ties a little more
thoughtfully:

SELECT fs1 . *
FROM `folderstatus` fs1
LEFT JOIN folderstatus fs2 ON fs2.idfolderstatus <> fs1.idfolderstatus
AND fs1.idfolder = fs2.idfolder
AND fs2.idtypestatus > fs1.idtypestatus
WHERE isnull( fs2.idtypestatus )
GROUP BY fs1.idfolder
ORDER BY fs1.idfolder

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #12 (permalink)  
Old 02-28-2008, 10:26 AM
Bob Bedford
 
Posts: n/a
Default Re: group by problem

> Anyway, here's an example of what I'm talking about. Incidentally,
> this is an example of a groupwise max problem.
> You may find it helpful to include fs2.* in the SELECT part of the
> query in order to better understand what's going on.
> I've tried to use your naming convention but I probably screwed up
> somewhere. The GROUP BY part of this statement is a bit of a
> nonsense. It just says 'In the event of a tie, pick one. I don't care
> which.' In reality you'd probably want to handle ties a little more
> thoughtfully:
>
> SELECT fs1 . *
> FROM `folderstatus` fs1
> LEFT JOIN folderstatus fs2 ON fs2.idfolderstatus <> fs1.idfolderstatus
> AND fs1.idfolder = fs2.idfolder
> AND fs2.idtypestatus > fs1.idtypestatus
> WHERE isnull( fs2.idtypestatus )
> GROUP BY fs1.idfolder
> ORDER BY fs1.idfolder


Great it works !

Now the pain will be to transpose to my DB with at least 20 linked
table...but I got the idea.

Thanks for your help.

Bob



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 05:14 PM.


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