Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select row with the latest timestamp from duplicated rows in a database table?

I have a table with duplicate & triplicate rows - how do I select the rows that are duplicated but have the latest timestamp as well as the un-duped rows?

 -------------------------------------
| pk_id | user_id | some_timestamp    |
|-------------------------------------|
| 1     | 123     | 10-Jun-12 14.30   |
| 2     | 123     | 19-Jun-12 21.50   |
| 3     | 567     | 10-Jun-12 09.23   |
| 4     | 567     | 12-Jun-12 09.45   |
| 5     | 567     | 13-Jun-12 08.40   |
| 6     | 890     | 13-Jun-12 08.44   |
 -------------------------------------

So that I end up with:

 -------------------------------------
| pk_id | user_id | some_timestamp    |
|-------------------------------------|
| 2     | 123     | 19-Jun-12 21.50   |
| 5     | 567     | 13-Jun-12 08.40   |
| 6     | 890     | 13-Jun-12 08.44   |
 -------------------------------------
like image 960
blank Avatar asked Dec 31 '12 09:12

blank


People also ask

How do I get the latest timestamp record in SQL?

To get the last updated record in SQL Server: We can write trigger (which automatically fires) i.e. whenever there is a change (update) that occurs on a row, the “lastupdatedby” column value should get updated by the current timestamp.

How do I select a date from a timestamp in SQL?

In MySQL, use the DATE() function to retrieve the date from a datetime or timestamp value. This function takes only one argument – either an expression which returns a date/datetime/ timestamp value or the name of a timestamp/datetime column.

How do I do a timestamp in SQL?

The basic syntax of “timestamp” data type in SQL is as follows : Timestamp 'date_expression time_expression'; A valid timestamp data expression consists of a date and a time, followed by an optional BC or AD.

What is the query to fetch the last record from the table?

To get the last record, the following is the query. mysql> select *from getLastRecord ORDER BY id DESC LIMIT 1; The following is the output. The above output shows that we have fetched the last record, with Id 4 and Name Carol.


3 Answers

SELECT * FROM ( 
SELECT pk_id, 
       user_id, 
       some_timestamp, 
       ROW_NUMBER() OVER (PARTITION BY user_id ORDER BY some_timestamp DESC) col 
FROM table) x  
WHERE x.col = 1
like image 140
Aditya Jain Avatar answered Nov 13 '22 18:11

Aditya Jain


try this

select * from table 
where some_timestamp 
   in (select max(some_timestamp)
       from table group by user_id)
like image 36
SRIRAM Avatar answered Nov 13 '22 17:11

SRIRAM


Try this, I made a SQLFIDDLE which returns the correct set of data

SELECT * FROM YourTable AS T1
INNER JOIN
( SELECT user_id , MAX(some_timestamp) AS some_timestamp FROM YourTable 
GROUP BY user_id
) AS T2
ON T1.User_Id = T2.User_Id AND T1.some_timestamp = T2.some_timestamp
ORDER BY 1

http://sqlfiddle.com/#!6/f7bba/6

like image 45
jazzytomato Avatar answered Nov 13 '22 18:11

jazzytomato