Deleting Rows in a table..

  • 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.

  • 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

  • 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

  • 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

    Β 

  • 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 Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • Hey Sean Lange,

    thanks a lot for sharing your experience and thoughts here.

    and for the solution as well.

  • 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.

Viewing 7 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic. Login to reply