Unix Technical Forum

SEO

vBulletin Search Engine Optimization


Go Back   Unix Technical Forum > Database Server Software > MySQL

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-28-2008, 09:26 AM
Bob Bedford
 
Posts: n/a
Default group by problem

Hi all, on a query I've this result:
69 86
77 84
112 91
114 91
119 91
124 92
125 92
133 93
....

first is the idfolder, second is the idclient.

Now When I do a group by idfolder, I've a total of 77 rows (see the result
above for the first part). If I do a group by idclient, then I only get 3
results

Why ? Have you an idea ?

Here is the query:
select folder.idfolder, folder.idclient from folderstatus
inner join statustype on folderstatus.idstatustype=statustype.idstatus
inner join folder on folderstatus.iddossier = folder.idfolder
group by folder.idfolder
having max(idstatustype) = (select idstatus from statustype where
statustype.name like '%search%' limit 0,1)

if I do a group by folder.idclient, then I only get 3 rows instead of about
50.

Bob



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

On 20 Mar, 15:38, "Bob Bedford" <b...@bedford.com> wrote:
> Hi all, on a query I've this result:
> 69 86
> 77 84
> 112 91
> 114 91
> 119 91
> 124 92
> 125 92
> 133 93
> ...
>
> first is the idfolder, second is the idclient.
>
> Now When I do a group by idfolder, I've a total of 77 rows (see the result
> above for the first part). If I do a group by idclient, then I only get 3
> results
>
> Why ? Have you an idea ?
>
> Here is the query:
> select folder.idfolder, folder.idclient from folderstatus
> inner join statustype on folderstatus.idstatustype=statustype.idstatus
> inner join folder on folderstatus.iddossier = folder.idfolder
> group by folder.idfolder
> having max(idstatustype) = (select idstatus from statustype where
> statustype.name like '%search%' limit 0,1)
>
> if I do a group by folder.idclient, then I only get 3 rows instead of about
> 50.
>
> Bob


Not easy to tell if we don't know what data is in he tables.

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

> Not easy to tell if we don't know what data is in he tables.

I can't export datas as they are confidential.
anyway the result are strange when I group by a field like it doesn't take
all results.
Is the problem from "having" ?



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

On Mar 20, 4:01 pm, "Bob Bedford" <b...@bedford.com> wrote:
> > Not easy to tell if we don't know what data is in he tables.

>
> I can't export datas as they are confidential.
> anyway the result are strange when I group by a field like it doesn't take
> all results.
> Is the problem from "having" ?


Why not present some dummy data (5 - 10 rows, say) together with the
results you'd expect from that data. Then we'd have a chance, (but
don't make it too easy!)

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

On Tue, 20 Mar 2007 16:38:36 +0100, Bob Bedford wrote:
> Hi all, on a query I've this result:
> 69 86
> 77 84
> 112 91
> 114 91
> 119 91
> 124 92
> 125 92
> 133 93
> ...
>
> first is the idfolder, second is the idclient.
>
> Now When I do a group by idfolder, I've a total of 77 rows (see the result
> above for the first part). If I do a group by idclient, then I only get 3
> results
>
> Why ? Have you an idea ?
>
> Here is the query:
> select folder.idfolder, folder.idclient from folderstatus
> inner join statustype on folderstatus.idstatustype=statustype.idstatus
> inner join folder on folderstatus.iddossier = folder.idfolder
> group by folder.idfolder
> having max(idstatustype) = (select idstatus from statustype where
> statustype.name like '%search%' limit 0,1)
>
> if I do a group by folder.idclient, then I only get 3 rows instead of about
> 50.


You haven't told us anything about your statustype table, its contents,
etc, yet, and you're selecting only SOME of the records, based on that.
How many, we certainly cannot say.

--
Cunningham's Second Law:
It's always more complex than you expect, even when you take
Cunningham's Second Law into account.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 02-28-2008, 09:26 AM
Bob Bedford
 
Posts: n/a
Default Re: group by problem

as a general request, here is the script:
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for client
-- ----------------------------
CREATE TABLE `client` (
`idclient` mediumint(8) unsigned NOT NULL auto_increment,
`clientname` varchar(50) NOT NULL default '',
PRIMARY KEY (`idclient`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

-- ----------------------------
-- Table structure for folder
-- ----------------------------
CREATE TABLE `folder` (
`idfolder` mediumint(8) unsigned NOT NULL auto_increment,
`datefolder` date default NULL,
`idperson` mediumint(8) unsigned NOT NULL default '0',
`idclient` mediumint(9) NOT NULL default '0',
PRIMARY KEY (`idfolder`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

-- ----------------------------
-- Table structure for folderstatus
-- ----------------------------
CREATE TABLE `folderstatus` (
`idfolderstatus` mediumint(8) unsigned NOT NULL auto_increment,
`idfolder` mediumint(8) unsigned NOT NULL default '0',
`idtypestatus` mediumint(8) unsigned NOT NULL default '0',
PRIMARY KEY (`idfolderstatus`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

-- ----------------------------
-- Table structure for person
-- ----------------------------
CREATE TABLE `person` (
`idperson` mediumint(8) unsigned NOT NULL auto_increment,
`personname` varchar(50) NOT NULL default '',
PRIMARY KEY (`idperson`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

-- ----------------------------
-- Table structure for typestatus
-- ----------------------------
CREATE TABLE `typestatus` (
`idtypestatus` mediumint(8) unsigned NOT NULL auto_increment,
`typestatus` varchar(50) NOT NULL default '',
`idcheck` tinyint(1) unsigned NOT NULL default '0',
PRIMARY KEY (`idtypestatus`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

-- ----------------------------
-- Records
-- ----------------------------
INSERT INTO `client` VALUES ('1', 'client1');
INSERT INTO `client` VALUES ('2', 'client2');
INSERT INTO `client` VALUES ('3', 'client3');
INSERT INTO `client` VALUES ('4', 'client4');
INSERT INTO `client` VALUES ('5', 'client5');
INSERT INTO `folder` VALUES ('1', '2006-01-01', '1', '1');
INSERT INTO `folder` VALUES ('2', '2006-02-05', '1', '3');
INSERT INTO `folder` VALUES ('3', '2006-03-08', '2', '1');
INSERT INTO `folder` VALUES ('4', '2006-03-09', '2', '2');
INSERT INTO `folder` VALUES ('5', '2006-05-05', '2', '3');
INSERT INTO `folder` VALUES ('6', '2006-12-08', '3', '2');
INSERT INTO `folderstatus` VALUES ('1', '1', '1');
INSERT INTO `folderstatus` VALUES ('2', '1', '2');
INSERT INTO `folderstatus` VALUES ('3', '1', '3');
INSERT INTO `folderstatus` VALUES ('4', '2', '1');
INSERT INTO `folderstatus` VALUES ('5', '2', '3');
INSERT INTO `folderstatus` VALUES ('6', '2', '4');
INSERT INTO `folderstatus` VALUES ('7', '3', '4');
INSERT INTO `folderstatus` VALUES ('8', '3', '1');
INSERT INTO `folderstatus` VALUES ('9', '4', '1');
INSERT INTO `folderstatus` VALUES ('10', '4', '4');
INSERT INTO `folderstatus` VALUES ('11', '4', '3');
INSERT INTO `folderstatus` VALUES ('12', '1', '3');
INSERT INTO `folderstatus` VALUES ('13', '1', '3');
INSERT INTO `person` VALUES ('1', 'aaaaa');
INSERT INTO `person` VALUES ('2', 'bbbbbb');
INSERT INTO `person` VALUES ('3', 'cccccc');
INSERT INTO `person` VALUES ('4', 'ddddd');
INSERT INTO `person` VALUES ('5', 'eeeeee');
INSERT INTO `person` VALUES ('6', 'ffffff');
INSERT INTO `person` VALUES ('7', 'gggggg');
INSERT INTO `typestatus` VALUES ('1', 'status1', '1');
INSERT INTO `typestatus` VALUES ('2', 'status2', '1');
INSERT INTO `typestatus` VALUES ('3', 'status3', '1');
INSERT INTO `typestatus` VALUES ('4', 'status4', '0');
INSERT INTO `typestatus` VALUES ('5', 'status5', '1');

The query (seems to work)
select person.idperson,
folder.datefolder, folder.idclient, folder.idfolder
from folderstatus
inner join typestatus on folderstatus.idtypestatus=typestatus.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
group by folder.idfolder
having (max(folderstatus.idtypestatus) = (select idtypestatus from
typestatus where typestatus.typestatus like '%4%' limit 0,1)
and folder.datefolder between '2006-01-01' and '2007-01-01')

But what I don't know how to do now:
I'd like all the records from folderstatus where last (I mean bigger
idfolderstatus) for every folder linked to the typestatus has a
typestatus.idcheck to 0.

Let's explain again in pseudo-code:
select * from folderstatus inner join typestatus where folderstatus =
max(folderstatus for this folder) and folderstatus.idcheck = 0
So if max(folderstatus) = 4 in this case, then return idfolder (as idcheck
in typestatus = 0) else don't return the row.
How to do this ?

Thanks for help.

Bob



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

On Mar 20, 7:43 pm, "Bob Bedford" <b...@bedford.com> wrote:
> as a general request, here is the script:
> SET FOREIGN_KEY_CHECKS=0;
> -- ----------------------------
> -- Table structure for client
> -- ----------------------------
> CREATE TABLE `client` (
> `idclient` mediumint(8) unsigned NOT NULL auto_increment,
> `clientname` varchar(50) NOT NULL default '',
> PRIMARY KEY (`idclient`)
> ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
>
> -- ----------------------------
> -- Table structure for folder
> -- ----------------------------
> CREATE TABLE `folder` (
> `idfolder` mediumint(8) unsigned NOT NULL auto_increment,
> `datefolder` date default NULL,
> `idperson` mediumint(8) unsigned NOT NULL default '0',
> `idclient` mediumint(9) NOT NULL default '0',
> PRIMARY KEY (`idfolder`)
> ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
>
> -- ----------------------------
> -- Table structure for folderstatus
> -- ----------------------------
> CREATE TABLE `folderstatus` (
> `idfolderstatus` mediumint(8) unsigned NOT NULL auto_increment,
> `idfolder` mediumint(8) unsigned NOT NULL default '0',
> `idtypestatus` mediumint(8) unsigned NOT NULL default '0',
> PRIMARY KEY (`idfolderstatus`)
> ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
>
> -- ----------------------------
> -- Table structure for person
> -- ----------------------------
> CREATE TABLE `person` (
> `idperson` mediumint(8) unsigned NOT NULL auto_increment,
> `personname` varchar(50) NOT NULL default '',
> PRIMARY KEY (`idperson`)
> ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
>
> -- ----------------------------
> -- Table structure for typestatus
> -- ----------------------------
> CREATE TABLE `typestatus` (
> `idtypestatus` mediumint(8) unsigned NOT NULL auto_increment,
> `typestatus` varchar(50) NOT NULL default '',
> `idcheck` tinyint(1) unsigned NOT NULL default '0',
> PRIMARY KEY (`idtypestatus`)
> ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
>
> -- ----------------------------
> -- Records
> -- ----------------------------
> INSERT INTO `client` VALUES ('1', 'client1');
> INSERT INTO `client` VALUES ('2', 'client2');
> INSERT INTO `client` VALUES ('3', 'client3');
> INSERT INTO `client` VALUES ('4', 'client4');
> INSERT INTO `client` VALUES ('5', 'client5');
> INSERT INTO `folder` VALUES ('1', '2006-01-01', '1', '1');
> INSERT INTO `folder` VALUES ('2', '2006-02-05', '1', '3');
> INSERT INTO `folder` VALUES ('3', '2006-03-08', '2', '1');
> INSERT INTO `folder` VALUES ('4', '2006-03-09', '2', '2');
> INSERT INTO `folder` VALUES ('5', '2006-05-05', '2', '3');
> INSERT INTO `folder` VALUES ('6', '2006-12-08', '3', '2');
> INSERT INTO `folderstatus` VALUES ('1', '1', '1');
> INSERT INTO `folderstatus` VALUES ('2', '1', '2');
> INSERT INTO `folderstatus` VALUES ('3', '1', '3');
> INSERT INTO `folderstatus` VALUES ('4', '2', '1');
> INSERT INTO `folderstatus` VALUES ('5', '2', '3');
> INSERT INTO `folderstatus` VALUES ('6', '2', '4');
> INSERT INTO `folderstatus` VALUES ('7', '3', '4');
> INSERT INTO `folderstatus` VALUES ('8', '3', '1');
> INSERT INTO `folderstatus` VALUES ('9', '4', '1');
> INSERT INTO `folderstatus` VALUES ('10', '4', '4');
> INSERT INTO `folderstatus` VALUES ('11', '4', '3');
> INSERT INTO `folderstatus` VALUES ('12', '1', '3');
> INSERT INTO `folderstatus` VALUES ('13', '1', '3');
> INSERT INTO `person` VALUES ('1', 'aaaaa');
> INSERT INTO `person` VALUES ('2', 'bbbbbb');
> INSERT INTO `person` VALUES ('3', 'cccccc');
> INSERT INTO `person` VALUES ('4', 'ddddd');
> INSERT INTO `person` VALUES ('5', 'eeeeee');
> INSERT INTO `person` VALUES ('6', 'ffffff');
> INSERT INTO `person` VALUES ('7', 'gggggg');
> INSERT INTO `typestatus` VALUES ('1', 'status1', '1');
> INSERT INTO `typestatus` VALUES ('2', 'status2', '1');
> INSERT INTO `typestatus` VALUES ('3', 'status3', '1');
> INSERT INTO `typestatus` VALUES ('4', 'status4', '0');
> INSERT INTO `typestatus` VALUES ('5', 'status5', '1');
>
> The query (seems to work)
> select person.idperson,
> folder.datefolder, folder.idclient, folder.idfolder
> from folderstatus
> inner join typestatus on folderstatus.idtypestatus=typestatus.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
> group by folder.idfolder
> having (max(folderstatus.idtypestatus) = (select idtypestatus from
> typestatus where typestatus.typestatus like '%4%' limit 0,1)
> and folder.datefolder between '2006-01-01' and '2007-01-01')
>
> But what I don't know how to do now:
> I'd like all the records from folderstatus where last (I mean bigger
> idfolderstatus) for every folder linked to the typestatus has a
> typestatus.idcheck to 0.
>
> Let's explain again in pseudo-code:
> select * from folderstatus inner join typestatus where folderstatus =
> max(folderstatus for this folder) and folderstatus.idcheck = 0
> So if max(folderstatus) = 4 in this case, then return idfolder (as idcheck
> in typestatus = 0) else don't return the row.
> How to do this ?
>
> Thanks for help.
>
> Bob


Have`you tried looking at the ubiquitous "strawberry query" in this
and other newsgroups?

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

tried this for my other post, but return 0 values:

select person.idperson, folderstatus.idtypestatus,typestatus.idcheck,
folder.datefolder, folder.idclient, 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
group by folder.idfolder
having (folderstatus.idtypestatus = max(folderstatus.idtypestatus) and
typestatus.idcheck = 0)



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

Can't find in this NG. Google didn't help much.



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

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



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 07:13 PM.


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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589