Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

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?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://155136]
help
Chatterbox?
[GrandFather]: It's a good day end here Corion. The start was somewhat less than average!
[Corion]: GrandFather: All's well that ends well? ;)
[GrandFather]: I'm fighting with a third party device our software is to support. The documentation for the device's SDK is quite a lot less than average and most of today was spent ...
[GrandFather]: discovering that one of the sensors for the device lies about the gain range it is using!
[GrandFather]: However, by the end of the day I had discovered its deceptions and now have it working correctly, so yes, all's well that ends well. :-D
[Corion]: GrandFather: Ah, (hardware) APIs - I have a similar situation with Chrome and its API... It is fairly underdocumented and I guess I have to hunt...
[Corion]: ... down supposedly working code to find out what I'm supposed to do
[GrandFather]: I haven't any "working code" to inspect! I have to find ways to generate reference signals then check the numbers I get at the far end match.

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2017-08-24 07:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Who is your favorite scientist and why?



























    Results (365 votes). Check out past polls.

    Notices?