SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Retrieve results from table with latest timestamp


Retrieve results from table with latest timestamp

Author
Message
dsrapid
dsrapid
SSC-Enthusiastic
SSC-Enthusiastic (123 reputation)SSC-Enthusiastic (123 reputation)SSC-Enthusiastic (123 reputation)SSC-Enthusiastic (123 reputation)SSC-Enthusiastic (123 reputation)SSC-Enthusiastic (123 reputation)SSC-Enthusiastic (123 reputation)SSC-Enthusiastic (123 reputation)

Group: General Forum Members
Points: 123 Visits: 82
Hi,

I have a table with syn_trans with below cols:
(
Id integer(10),
Name varchar2(100),
seq_no integer(10),
seq_name varchar2(100),
gp_name varchar2(100)
)

There is no unique/primary key on the table.
A row exists in table with below values:
INSERT INTO syn_trans VALUES(60175,'AccountOverview',0,'Signon','StepGp');

Two days later I again execute the same insert statement and now there exists two records for the ID=60175

I want to retrieve the entry with latest timestamp or in other words I want to delete the entry for this ID which I added second time.

Plz provide the SQL for this.
Thanks in advance.
Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (92K reputation)SSC Guru (92K reputation)SSC Guru (92K reputation)SSC Guru (92K reputation)SSC Guru (92K reputation)SSC Guru (92K reputation)SSC Guru (92K reputation)SSC Guru (92K reputation)

Group: General Forum Members
Points: 92369 Visits: 38954
Nidhi G (12/24/2012)
Hi,

I have a table with syn_trans with below cols:
(
Id integer(10),
Name varchar2(100),
seq_no integer(10),
seq_name varchar2(100),
gp_name varchar2(100)
)

There is no unique/primary key on the table.
A row exists in table with below values:
INSERT INTO syn_trans VALUES(60175,'AccountOverview',0,'Signon','StepGp');

Two days later I again execute the same insert statement and now there exists two records for the ID=60175

I want to retrieve the entry with latest timestamp or in other words I want to delete the entry for this ID which I added second time.

Plz provide the SQL for this.
Thanks in advance.


First, there is nothing in the table structure to tell which record is inserted first. Second, if they are completely identical, does it really matter which of the duplicate records gets deleted?

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
dsrapid
dsrapid
SSC-Enthusiastic
SSC-Enthusiastic (123 reputation)SSC-Enthusiastic (123 reputation)SSC-Enthusiastic (123 reputation)SSC-Enthusiastic (123 reputation)SSC-Enthusiastic (123 reputation)SSC-Enthusiastic (123 reputation)SSC-Enthusiastic (123 reputation)SSC-Enthusiastic (123 reputation)

Group: General Forum Members
Points: 123 Visits: 82
How do I delete one entry then?
Bhuvnesh
Bhuvnesh
SSChampion
SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)

Group: General Forum Members
Points: 12874 Visits: 4077
Nidhi G (12/24/2012)
How do I delete one entry then?
this query will help you to remove duplicates
delete t from 
(
select ROW_NUMBER() over (partition by Id order by Id ) as rank1,* from syn_trans) t
where t.rank1 <> 1




THOUGH it doesnt guarantee about the newest or oldest record deletion because you are not maintainig that OR you can add one another column like created_date (timestamp) or any identity column to maintain the sequence.

-------Bhuvnesh----------
I work only to learn Sql Server...though my company pays me for getting their stuff done;-)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search