Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

Re: Re: MySQL question

by Kozz (Friar)
on Mar 28, 2002 at 22:21 UTC ( #155136=note: print w/replies, xml ) Need Help??

in reply to Re: MySQL question
in thread MySQL question

In the spirit of sportsman-like conduct, I ++'ed your post. Mostly the reason I venture to ask this question here is because it's a reasonable forum with intelligent persons of moderate tempers. ;) I don't get along with usenet very well, some IRC channels seem to smack of elitism, and don't know of any other online forums that would be better suited to answer my question.

Anyhow, to answer your question, I was hoping to get one row per distinct doc_num whose version was the max() of all versions for that doc_num, and whose title corresponded to the record with the max version:

title | doc_num | version -------+---------+-------- foo | 1234 | 2 baz | 5678 | 1
Compare my chart above to the "original" and you may see what I mean.

Anyhow, my apologies for tempting fate in this manner. If I've overstepped my bounds by replying to your post, I once again apologize. But if you find the breath to chastize me once more, I also welcome links to any Database-related forums. ;)

Replies are listed 'Best First'.
•Re: Re: Re: MySQL question
by merlyn (Sage) on Mar 28, 2002 at 22:28 UTC
    Ahh. Then again, with a real database with subselects, that'd be:
    SELECT title, doc_num, version FROM atable WHERE (doc_num, version) IN ( SELECT doc_num, MAX(version) FROM atable GROUP BY doc_num );
    Again, this won't work in puny MySQL. PostgreSQL or better, baby!

    -- Randal L. Schwartz, Perl hacker

      If Kozz cannot upgrade to PostgreSQL for some reason couldn't Kozz ape a subselect with by creating a tempory table "ttable" and then join "atable" with that? sonething like this ...
      create table ttable as SELECT doc_num, MAX(version) FROM atable GROUP BY doc_num; SELECT title, doc_num, version FROM atable a, ttable t WHERE a.doc_num = t.doc_num and a.version = t.version;

      ... I don't know about the other monks I find SQL
      fun like its a weird puzzle.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://155136]
and John Coltrane plays...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (3)
As of 2017-02-24 01:59 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (351 votes). Check out past polls.