Solved

Need SQL help

Posted on 2011-03-23
1
188 Views
Last Modified: 2012-05-11
I am trying to write a sql statement perhaps CTE that will find the earliest contact_date or there is a diferrence between the episde dates of greater than six months. Should I do a stored procedure an break it up?    Here's what I have so far. Thanks

;with CTEMembersLBP as
 (SELECT *, row_number() over(partition by Pat_ID order by Pat_ID, contact_date asc) as Dup
FROM dbo.GR_LBP_DX_2010)
Delete from CTEmembersLBP where Dup > 1  
0
Comment
Question by:Wonderwall
1 Comment
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
Comment Utility
the ORDER BY does not need the Pat_ID ...

;with CTEMembersLBP as
 (SELECT *, row_number() over(partition by Pat_ID order by contact_date asc) as Dup
FROM dbo.GR_LBP_DX_2010)
Delete from CTEmembersLBP where Dup > 1  

apart from that, if you have a (clustered) index on Pat_ID + contact_date, this shall work best.

otherwise, if you really have to delete massive data, you might insert the data you want to retain into a temp table, truncate the original table, then insert back the data you want to retain from the temp table
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now