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

Deleting Rows in a table.. Expand / Collapse
Author
Message
Posted Thursday, April 25, 2013 11:29 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, April 29, 2013 12:44 AM
Points: 18, Visits: 135
I have a table like that
MemberId(F.K) ! Event ! Event Date
1 'Test Event1' "2012-03-20 05:39:51"
1 'Test Event1' "2012-03-21 05:39:51"
1 'Test Event1' "2012-03-22 05:39:51"
3 'Test Event3' "2012-03-23 05:39:51"
2 'Test Event2' "2012-03-24 05:39:51"
2 'Test Event2' "2012-03-19 05:39:51"
1 'Test Event1' "2012-03-23 05:49:51"
3 'Test Event3' "2012-03-23 05:49:51"
4 'Test Event4' "2012-03-27 05:39:51"
3 'Test Event3' "2012-03-21 05:39:51"
. . .
. .
and what i require is to keep only two latest events for each members and to delete the rest ones.
i.e.
1 'Test Event1' "2012-03-23 05:49:51"
1 'Test Event1' "2012-03-22 05:39:51"
2 'Test Event2' "2012-03-24 05:39:51"
2 'Test Event2' "2012-03-19 05:39:51"
3 'Test Event3' "2012-03-23 05:39:51"
3 'Test Event3' "2012-03-23 05:49:51"
4 'Test Event4' "2012-03-27 05:39:51"

I have an idea of doing like that by using CTE or by using RowNumbers and Partitions but i have to avoid the power of DBMS , Cursors and to write a pure SQL Query for that ,
any help will be appreciated.
Thanks.
Post #1446798
Posted Friday, April 26, 2013 5:11 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 9:54 AM
Points: 6,813, Visits: 14,028
faheemahmad14 (4/25/2013)
I have a table like that
MemberId(F.K) ! Event ! Event Date
1 'Test Event1' "2012-03-20 05:39:51"
1 'Test Event1' "2012-03-21 05:39:51"
1 'Test Event1' "2012-03-22 05:39:51"
3 'Test Event3' "2012-03-23 05:39:51"
2 'Test Event2' "2012-03-24 05:39:51"
2 'Test Event2' "2012-03-19 05:39:51"
1 'Test Event1' "2012-03-23 05:49:51"
3 'Test Event3' "2012-03-23 05:49:51"
4 'Test Event4' "2012-03-27 05:39:51"
3 'Test Event3' "2012-03-21 05:39:51"
. . .
. .
and what i require is to keep only two latest events for each members and to delete the rest ones.
i.e.
1 'Test Event1' "2012-03-23 05:49:51"
1 'Test Event1' "2012-03-22 05:39:51"
2 'Test Event2' "2012-03-24 05:39:51"
2 'Test Event2' "2012-03-19 05:39:51"
3 'Test Event3' "2012-03-23 05:39:51"
3 'Test Event3' "2012-03-23 05:49:51"
4 'Test Event4' "2012-03-27 05:39:51"

I have an idea of doing like that by using CTE or by using RowNumbers and Partitions but i have to avoid the power of DBMS , Cursors and to write a pure SQL Query for that ,
any help will be appreciated.
Thanks.


Are you using MS SQL Server for this?
If so, which version?
If someone offers a solution using a cursor and it's the fastest solution that anyone here can offer, would it be acceptable to you?
If not, then why?


“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Post #1446898
Posted Friday, April 26, 2013 6:44 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, April 29, 2013 12:44 AM
Points: 18, Visits: 135
Actually this issue was asked in an interview of mine and my answers were to use Row_Numbers, Cursors , functions etc . but they forced me to provide a solution without native powers of tool and to do this by using simple SQL.
i threw this question here for my knowledge if there exists a solution for that.

thanks
Post #1446933
Posted Friday, April 26, 2013 11:11 AM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Sunday, September 29, 2013 1:24 AM
Points: 429, Visits: 1,721

IF OBJECT_ID('tempdb..#TempTable') IS NOT NULL
DROP TABLE #TempTable

CREATE TABLE #TempTable (
[ID] INT IDENTITY(1,1) NOT NULL,
[MemberID] INT NULL,
[Event] NVARCHAR(50) NULL,
[EventDate] DATETIME NULL,
PRIMARY KEY (ID))

INSERT INTO #TempTable
SELECT 1,'Test Event1','2012-03-20 05:39:51' UNION ALL
SELECT 1,'Test Event1','2012-03-21 05:39:51' UNION ALL
SELECT 1,'Test Event1','2012-03-22 05:39:51' UNION ALL
SELECT 3,'Test Event3','2012-03-23 05:39:51' UNION ALL
SELECT 2,'Test Event2','2012-03-24 05:39:51' UNION ALL
SELECT 2,'Test Event2','2012-03-19 05:39:51' UNION ALL
SELECT 1,'Test Event1','2012-03-23 05:49:51' UNION ALL
SELECT 3,'Test Event3','2012-03-23 05:49:51' UNION ALL
SELECT 4,'Test Event4','2012-03-27 05:39:51' UNION ALL
SELECT 3,'Test Event3','2012-03-21 05:39:51'



SELECT
r.MemberID
,r.Event
,r.EventDate
FROM
(
SELECT
ROW_NUMBER() OVER (PARTITION BY MemberID ORDER BY EventDate DESC) AS RowNum
,MemberID
,Event
,EventDate
FROM
#TempTable
) r
WHERE
RowNum <= 2



 
Post #1447104
Posted Friday, April 26, 2013 1:40 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 3:13 PM
Points: 13,085, Visits: 12,551
faheemahmad14 (4/26/2013)
Actually this issue was asked in an interview of mine and my answers were to use Row_Numbers, Cursors , functions etc . but they forced me to provide a solution without native powers of tool and to do this by using simple SQL.
i threw this question here for my knowledge if there exists a solution for that.

thanks


I would ask them if they are in the habit of routinely limiting developers to not using all the facets of a tool.

Obviously what they want to understand is your knowledge of sql. The question is a very poor one imho. If you ask an interview question and ask someone to write code you shouldn't dictate they can't use powerful features of the language.

This reminds me of a software developers test I took. They asked me to write a linked list in .NET, the instructions also explicitly stated NOT to use the native LinkedList object. I wrote the code example and about a half of diatribe about how ridiculous it is to ask to disallow using features of a language. Obviously, the reason the object was written into the framework is because it is a complete PITA to roll your own. Using windowed functions is much the same.

Venting aside, you can do this without using "Row_Numbers, Cursors , functions etc". I did use an outer apply but this goes back to my previous comment.

Much thanks to Steve Willis for putting together sample data and ddl.

delete #TempTable 
where ID not in
(
select x.ID
from #TempTable t
outer apply
(
select top 1 tt.*
from #TempTable tt
where tt.Event = t.Event
and tt.ID <> t.ID
order by tt.EventDate desc
) x
where x.ID is not null
group by x.ID
)

select * from #TempTable order by Event, EventDate desc



_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1447146
Posted Monday, April 29, 2013 12:46 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, April 29, 2013 12:44 AM
Points: 18, Visits: 135
Hey Sean Lange,
thanks a lot for sharing your experience and thoughts here.
and for the solution as well.
Post #1447373
Posted Tuesday, April 30, 2013 11:40 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Friday, August 30, 2013 1:37 PM
Points: 30, Visits: 17
Here's one way not using analytics or outer apply. At first I used a CTE but I removed that to use an inline view instead even though CTE's were not explicitly forbidden.

Btw, there could be any number of reasons to restrict the tools available for use. Sometimes code has to execute in a number of environments, sometimes maintainability will outweigh performance, especially in important code that executes only rarely, sometimes...well, sometimes just about anything will rear it's ugly head. So it's perfectly understandable that employers would want to test the flexibility and innovation of a potential developer.

This doesn't actually perform a delete, but that's a trivial modification. I just wanted to set up one possible solution within the constraints. It's written for SybaseIQ, but that's close enough to Sql Server's version of Transact-SQL to be clear.

create local temporary table #TestEvents (
MemberID int not null,
Event varchar( 50 ) not null,
EventDate datetime not null
);

insert into #TestEvents
select 1,'Test Event1','2012-03-20 05:39:51' union all
select 1,'Test Event1','2012-03-21 05:39:51' union all
select 1,'Test Event1','2012-03-22 05:39:51' union all
select 3,'Test Event3','2012-03-23 05:39:51' union all
select 2,'Test Event2','2012-03-24 05:39:51' union all
select 2,'Test Event2','2012-03-19 05:39:51' union all
select 1,'Test Event1','2012-03-23 05:49:51' union all
select 3,'Test Event3','2012-03-23 05:49:51' union all
select 4,'Test Event4','2012-03-27 05:39:51' union all
select 3,'Test Event3','2012-03-21 05:39:51';

select distinct t.*
from #TestEvents t
join(
select t1.MemberID, t1.Event, T1.EventDate as FirstDate, t2.EventDate as SecondDate
from #TestEvents t1
left join #TestEvents t2
on t2.MemberID = t1.MemberID
and t2.EventDate < t1.EventDate
where t1.EventDate =(
select Max( EventDate )
from #TestEvents
where MemberID = t1.MemberID)
and (t2.EventDate is null or t2.EventDate =(
select Max( EventDate )
from #TestEvents
where MemberID = t1.MemberID
and EventDate < t1.EventDate))
) t2
on t2.FirstDate = t.EventDate
or t2.SecondDate = t.EventDate
order by t.MemberID, t.EventDate desc;



TommCatt
In theory, there is no difference between theory and practice. In practice, there is.
Post #1448163
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse