Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Retrieve results from table with latest timestamp Expand / Collapse
Author
Message
Posted Monday, December 24, 2012 2:29 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, March 12, 2013 6:55 AM
Points: 33, 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.
Post #1399991
Posted Monday, December 24, 2012 3:35 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Yesterday @ 3:27 PM
Points: 20,734, Visits: 32,505
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?



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)
Post #1399997
Posted Monday, December 24, 2012 3:57 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, March 12, 2013 6:55 AM
Points: 33, Visits: 82
How do I delete one entry then?
Post #1400000
Posted Wednesday, December 26, 2012 12:00 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: 2 days ago @ 2:36 AM
Points: 2,840, Visits: 3,970
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
Post #1400154
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse