Solved

Need SQL help

Posted on 2011-03-23
1
203 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
ID: 35202400
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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SQL Encryption question 2 50
how many extra RAM for SQL server is needed 22 35
Sql Query join multiple table and distinct records 7 29
SQL Backup skipping a few tables 7 36
I've encountered valid database schemas that do not have a primary key.  For example, I use LogParser from Microsoft to push IIS logs into a SQL database table for processing and analysis.  However, occasionally due to user error or a scheduled task…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

862 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

25 Experts available now in Live!

Get 1:1 Help Now