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


Delete other than top 2 rows for each foreign key element in table.


Delete other than top 2 rows for each foreign key element in table.

Author
Message
faheemahmad14
faheemahmad14
SSC Rookie
SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)

Group: General Forum Members
Points: 34 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 for that ,
any help will be appreciated.
Thanks.
ChrisM@Work
ChrisM@Work
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16802 Visits: 19557
-- Always run a SELECT first to see which rows are affected.
-- This query should return the rows you want to keep
SELECT MemberId, [Event], [Event Date]
FROM (
SELECT MemberId, [Event], [Event Date],
rn = ROW_NUMBER() OVER (PARTITION BY MemberId ORDER BY [Event Date])
FROM MyTable
) d
WHERE rn < 3



“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
bitbucket-25253
bitbucket-25253
SSCrazy Eights
SSCrazy Eights (8K reputation)SSCrazy Eights (8K reputation)SSCrazy Eights (8K reputation)SSCrazy Eights (8K reputation)SSCrazy Eights (8K reputation)SSCrazy Eights (8K reputation)SSCrazy Eights (8K reputation)SSCrazy Eights (8K reputation)

Group: General Forum Members
Points: 8001 Visits: 25280
ChrisM@Work (4/25/2013)
-- Always run a SELECT first to see which rows are affected.
-- This query should return the rows you want to keep
SELECT MemberId, [Event], [Event Date]
FROM (
SELECT MemberId, [Event], [Event Date],
rn = ROW_NUMBER() OVER (PARTITION BY MemberId ORDER BY [Event Date])
FROM MyTable
) d
WHERE rn < 3

The above returns
MemberId Event Event_Date
1 Test Event1 2012-03-20 05:39:51.000
1 Test Event1 2012-03-21 05:39:51.000
2 Test Event2 2012-03-19 05:39:51.000
2 Test Event2 2012-03-24 05:39:51.000
3 Test Event3 2012-03-21 05:39:51.000
3 Test Event3 2012-03-23 05:39:51.000
4 Test Event4 2012-03-27 05:39:51.000
modifying this line of code:
rn = ROW_NUMBER() OVER (PARTITION BY MemberId ORDER BY [Event_Date] )
to be:
rn = ROW_NUMBER() OVER (PARTITION BY MemberId ORDER BY [Event_Date] DESC )

Returns:
MemberId Event Event_Date
1 Test Event1 2012-03-23 05:49:51.000
1 Test Event1 2012-03-22 05:39:51.000
2 Test Event2 2012-03-24 05:39:51.000
2 Test Event2 2012-03-19 05:39:51.000
3 Test Event3 2012-03-23 05:49:51.000
3 Test Event3 2012-03-23 05:39:51.000
4 Test Event4 2012-03-27 05:39:51.000



If everything seems to be going well, you have obviously overlooked something.

Ron

Please help us, help you -before posting a question please read

Before posting a performance problem please read
ChrisM@Work
ChrisM@Work
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16802 Visits: 19557
Thanks Ron.

“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 Rookie
SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)

Group: General Forum Members
Points: 34 Visits: 135
thanks a lot for your prompt response
but my requirement is not to use RowNumber or any other builtin function.
ChrisM@Work
ChrisM@Work
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16802 Visits: 19557
faheemahmad14 (4/25/2013)
thanks a lot for your prompt response
but my requirement is not to use RowNumber or any other builtin function.


ROW_NUMBER is ideal for this - can you explain why you can't use it? You've posted in the SQL Server 2008 forum section.

“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 Rookie
SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)

Group: General Forum Members
Points: 34 Visits: 135
I am sorry for that , i think i should post in some other forum.
thanks a lot guys for your responses.
Cadavre
Cadavre
Hall of Fame
Hall of Fame (4K reputation)Hall of Fame (4K reputation)Hall of Fame (4K reputation)Hall of Fame (4K reputation)Hall of Fame (4K reputation)Hall of Fame (4K reputation)Hall of Fame (4K reputation)Hall of Fame (4K reputation)

Group: General Forum Members
Points: 3982 Visits: 8472
Sample data, in case anyone else wants to have a go: -

SELECT [MemberId(F.K)], [Event], [Event Date]
INTO #yourTable
FROM (VALUES(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')
)a([MemberId(F.K)], [Event], [Event Date]);



Solution: -
SELECT [MemberId(F.K)], [Event], [Event Date]
FROM #yourTable a
WHERE [Event Date] IN (SELECT TOP 2 [Event Date]
FROM #yourTable b
WHERE a.[MemberId(F.K)] = b.[MemberId(F.K)] AND a.[Event Date] <= b.[Event Date]
ORDER BY [Event Date] DESC
);



Be aware that this is infinitely worse than using the ROW_NUMBER function.


Forever trying to learn

For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/

For better, quicker answers on SQL Server performance related questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/



If you litter your database queries with nolock query hints, are you aware of the side effects?
Try reading a few of these links...

(*) Missing rows with nolock
(*) Allocation order scans with nolock
(*) Consistency issues with nolock
(*) Transient Corruption Errors in SQL Server error log caused by nolock
(*) Dirty reads, read errors, reading rows twice and missing rows with nolock


Craig Wilkinson - Software Engineer
LinkedIn
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