Jump to content

[SOLVED] Selecting a max value among similar rows


aaronvegh

Recommended Posts

I have a MySQL (v. 5.067) table that I'd like to pull a particular value from. Consider this table:

 

mysql> describe repos;

+------------+--------------+------+-----+---------+----------------+

| Field      | Type        | Null | Key | Default | Extra          |

+------------+--------------+------+-----+---------+----------------+

| id        | int(11)      | NO  | PRI | NULL    | auto_increment |

| filename  | varchar(255) | YES  |    | NULL    |                |

| uuid      | varchar(255) | YES  |    | NULL    |                |

| revision  | int(11)      | YES  |    | NULL    |                |

| project_id | int(11)      | YES  |    | NULL    |                |

| message    | varchar(255) | YES  |    | NULL    |                |

| parent_id  | int(11)      | YES  |    | NULL    |                |

| path      | varchar(255) | YES  |    | NULL    |                |

| created_at | datetime    | YES  |    | NULL    |                |

| updated_at | datetime    | YES  |    | NULL    |                |

+------------+--------------+------+-----+---------+----------------+

 

There can be many rows that have an identical "filename" and "path" field. Each time a new file entry is made, the "revision" field will be incremented, so that you'll have rows like this:

 

mysql> select filename, path, revision from repos;

+---------------------------+-------------------------------+

| filename                  | path          | revision |

+---------------------------+---------------------------------------+----------+

| Whitby Chamber Emails.txt | /daytime tv/ |        1 |

| Whitby Chamber Emails.txt | /daytime tv/ |        2 |

| 2361853 EPO inv.pdf      | /daytime tv/ |        1 |

| 2361853 EPO inv.pdf      | /daytime tv/ |        2 |

+---------------------------+-------------------------------------------+

 

Basically, I want a query that would choose the max value for each distinct file. That select statement would produce:

 

+---------------------------+-------------------------------+

| filename                  | path          | revision |

+---------------------------+---------------------------------------+----------+

| Whitby Chamber Emails.txt | /daytime tv/ |        2 |

| 2361853 EPO inv.pdf      | /daytime tv/ |        2 |

+---------------------------+-------------------------------------------+

 

I've been banging my head against the wall on this one for a while now. Any help would be adored!

 

Thanks,

Aaron.

 

 

Link to comment
Share on other sites

Hmm, actually, this may not be solved. A further investigation showed that this select statement returns not the row with the highest revision number, but instead the earliest rows (those with the lowest revision numbers) with a new field showing the highest revision number. Here's that table from above again, but this time with the id field:

 

mysql> select filename, path, revision, id from repos;

+---------------------------+-------------------------------+

| filename                  | path          | revision | id

+---------------------------+---------------------------------------+----------+

| Whitby Chamber Emails.txt | /daytime tv/ |        1 | 8

| Whitby Chamber Emails.txt | /daytime tv/ |        2 | 9

| 2361853 EPO inv.pdf      | /daytime tv/ |        1 | 10

| 2361853 EPO inv.pdf      | /daytime tv/ |        2 | 11

+---------------------------+-------------------------------------------+

 

So I really want my select statement to return rows 9 and 11. Instead, I get:

 

mysql> select id, filename, max(path), max(revision) from repos group by filename;

+----+---------------------------+-------------------------------------+

| id | filename                  | max(path)        | max(revision) |

+----+---------------------------+-------------------------------------+

| 10 | 2361853 EPO inv.pdf      | /daytime tv/ |            2 |

|  8 | Whitby Chamber Emails.txt | /daytime tv/ |            2 |

+----+---------------------------+-------------------------------------+

 

It's returning the wrong rows with the right revision number. This is a partial solution, since I can then easily take that revision number and find the right row. But is it possible to do this in one query?

 

Thanks!

Aaron.

 

 

Link to comment
Share on other sites

How about:

 

select filename, max(path), max(revision), max(id)

from repos

group by filename

 

And, you might want to consider adding path to the group by (as fenway suggested). It's hard to tell from your data sample if filename is always 1 to 1 to path. Can you have the same filename in 2 diff paths ? If so, add path to the group by. Actually, probably better to do it regardless

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.