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


Deleting Rows in a table..


Deleting Rows in a table..

Author
Message
faheemahmad14
faheemahmad14
SSC Journeyman
SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)

Group: General Forum Members
Points: 82 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.
ChrisM@Work
ChrisM@Work
SSC-Forever
SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)

Group: General Forum Members
Points: 41813 Visits: 20007
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
faheemahmad14
faheemahmad14
SSC Journeyman
SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)

Group: General Forum Members
Points: 82 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
Steven Willis
Steven Willis
SSCrazy
SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)

Group: General Forum Members
Points: 2063 Visits: 1721


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





 
Sean Lange
Sean Lange
SSC Guru
SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)

Group: General Forum Members
Points: 63136 Visits: 17960
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. :-D


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 Modens 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)
faheemahmad14
faheemahmad14
SSC Journeyman
SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)

Group: General Forum Members
Points: 82 Visits: 135
Hey Sean Lange,
thanks a lot for sharing your experience and thoughts here.
and for the solution as well.
Thomas.Carr 15137
Thomas.Carr 15137
SSC Rookie
SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)

Group: General Forum Members
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.
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