If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > retrieving document details with highest version among the documents of same name

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-21-09, 05:59
Hunky Hunky is offline
Registered User
 
Join Date: Apr 2009
Posts: 4
retrieving document details with highest version among the documents of same name

I have a table with the following fields and records

DocName-Doctype-DocVersion-DocID
ABC HR 1.0 1
ABC HR 2.0 2
XYZ HR 1.0 3
XYZ HR 1.1 4
PQR FM 1.0 5

Now i have to write a query such that only DocName ABC and XYZ's highest version records get selected. The records are to be fetchd on a particular DocType (HR in this case)
the result should b like this

DocName-Doctype-DocVersion-DocID
ABC HR 2.0 2
XYZ HR 1.1 4

Any kind of help is highly appreciated.
Thanks in Advance
Reply With Quote
  #2 (permalink)  
Old 04-21-09, 07:10
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
Code:
SELECT t.DocName
     , t.Doctype
     , t.DocVersion
     , t.DocID
  FROM ( SELECT DocName 
              , MAX(DocVersion) AS max_version
           FROM daTable
         GROUP
             BY DocName ) AS m
INNER
  JOIN daTable AS t
    ON t.DocName = m.DocName
   AND t.DocVersion = m.max_version
 WHERE t.DocType = 'HR'
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 04-21-09, 08:29
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
Would it not be more efficient to move the where clause in to the subquery?

Note that this could return different results depending on the nature of your data: can one document name have more than one document type?

If so, and assuming you have the data
PQR HR 1.0 5
PQR FM 2.0 6
Would you want to return PQR in your result?

If the answer is yes, then put there where clause in the subquery.
If the answer is no, then leave it be
__________________
George
Twitter | Blog
Reply With Quote
  #4 (permalink)  
Old 04-24-09, 01:10
Hunky Hunky is offline
Registered User
 
Join Date: Apr 2009
Posts: 4
Thanks r937...

And for george..

In the scenario that u have given.. It should show both PQR
PQR HR 1.0 5
PQR FM 2.0 6

Bcoz here the document type is different and not same...
How can i modify the query for that...
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On