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 > Data Access, Manipulation & Batch Languages > ANSI SQL > Retrieving rows with 2 indexes

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-24-04, 11:47
nzwy1p nzwy1p is offline
Registered User
 
Join Date: Aug 2004
Location: Near a PC
Posts: 4
Retrieving rows with 2 indexes

I have a table that contains 2 keys (iId and iVersion) and some other data:

iId int(4) NOT NULL default '0',
iVersion int(4) NOT NULL default '0',
vchName varchar(50) default NULL

The data looks a little like this:

1|1|Fred
1|2|Fred edited once
1|3|Fred edited twice
2|1|Dave
2|2|Dave edited once
2|3|Dave edited twice

I need a sql statement that will return all columns of the latest row (based on the greatest iVersion value) for each unique iId in the table.

So using the data above it should return

1|3|Fred edited twice
2|3|Dave edited twice

Any help would be appreciated!
Reply With Quote
  #2 (permalink)  
Old 08-24-04, 12:18
urquel urquel is offline
Registered User
 
Join Date: Aug 2004
Posts: 330
select iId,
iVersion,
vchName
from <table_name>
group by iId
having iVersion = max(iVersion)
Reply With Quote
  #3 (permalink)  
Old 08-24-04, 13:04
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 5,171
Try:
Code:
select iId,
       iVersion,
       vchName
from   <table_name>
where (iId, iVersion) in
( select iId,
         max(iVersion) maxVer
  from   <table_name>
  group by iId
);
Or if using Oracle you could use the analytic functions.
__________________
Tony Andrews
http://tinyurl.com/tonyandrews
Reply With Quote
  #4 (permalink)  
Old 08-24-04, 14:13
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,605
I would suggest using:
Code:
SELECT *
   FROM myTable AS a
   WHERE  a.iVersion = (SELECT Max(b.iVersion)
      FROM myTable AS b
      WHERE  b.iId = a.iId)
-PatP
Reply With Quote
  #5 (permalink)  
Old 08-24-04, 15:28
nzwy1p nzwy1p is offline
Registered User
 
Join Date: Aug 2004
Location: Near a PC
Posts: 4
May have been that i was using MySQL but none of the above worked!

They did however help me to get some sql that does work....

select iId, vchName, MAX(iVersion) as iVersion
from <Table>
GROUP BY iId

Thanks.
Reply With Quote
  #6 (permalink)  
Old 08-24-04, 23:57
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
Quote:
Originally Posted by nzwy1p
select iId, vchName, MAX(iVersion) as iVersion
from <Table>
GROUP BY iId
you may think this works, but it doesn't

even the mysql docs tell you that this gives unpredictable results (holler if you need the link to the page in the docs where it explains this)

what you want is the vchName that comes from the row which has the largest iVersion, but this is not what you are getting, and if it looks like you are getting it, it is a fluke

you could just as easily get this instead --

1|3|Fred edited once
2|3|Dave

i'm sorry if this sounds like i'm dumping all over you, it's not your fault, it's mysql's fault for allowing non-standard sql to run (in any other database system, your query would generate a syntax error)

here's what you want, done without subqueries if you're not on 4.1 yet --
Code:
select X.iId 
     , X.iVersion 
     , X.vchName 
  from yourtable as X
inner
  join yourtable as Y
    on X.iId
     = Y.iId  
group
    by X.iId 
     , X.iVersion 
     , X.vchName 
having X.iVersion
     = max(Y.iVersion)
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
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