user2998990
user2998990

Reputation: 980

Get latest rows from my sql database

I have a table where there in rows there is a column called version. I have 2 same entries with 1 column say abc(unique) in all the same rows. I have 2 rows as follows

ID|Name|Version|Unique_Id
-------------------------
1 |abc |1      | 23
2 |abc1|2      |23
3 |xyz |1      |21
4 |tre |1      |20

I want the result as

ID|Name|Version|Unique_Id
-------------------------
2 |abc1|2      |23
3 |xyz |1      |21
4 |tre |1      |20

I have tried grouping by Unique_Id, the result is as follows

ID|Name|Version|Unique_Id
-------------------------
1 |abc |1      | 23
3 |xyz |1      |21
4 |tre |1      |20

Following is the query I am using

SELECT *  FROM test
group by Unique_Id
order by Version desc;

I want latest(top order by desc) of each each rows. Please help. How can i achieve that.

Upvotes: 0

Views: 57

Answers (1)

Adriaan Stander
Adriaan Stander

Reputation: 166606

How about something like

INSERT INTO tbllogs 
            (logorigin, 
             logaction, 
             loguser, 
             logdate, 
             logoutcome) 
VALUES      (:origin, 
             :action, 
             :user, 
             :dt, 
             :outcome) 

Use a sub select to determine the id and its max version number, then join back to the original table to retrieve the other values.

SQL Fiddle DEMO

Upvotes: 3

Related Questions