Solved

SQL Server Select Query to remove duplicate records and compute a new column

Posted on 2014-01-20
7
532 Views
Last Modified: 2014-02-01
I have a table with the columns below and this table that contains some duplicate Address records.  I need a SQL select query that this extract all the columns below and remove the records that contain duplicate records based on the address column.   I also need this query to compute a new date column for me called lead date.  This is computed by adding 7 days to the RecordingDate column.

Address      
City      
State
Zip
RecordingDate

thanks,
0
Comment
Question by:hojohappy
7 Comments
 
LVL 3

Assisted Solution

by:Sreeram
Sreeram earned 250 total points
Comment Utility
Hi

You can use the Below statement

RecordingDate should be as an datetime column so that below query will work Or convert RecordingDate to datetime data format in query itself

Query:

Select Distinct(Address),City,state,zip,RecordingDate,((RecordingDate)+7) as Leaddate from TableA
0
 
LVL 38

Accepted Solution

by:
Jim P. earned 250 total points
Comment Utility
Here's a query that will give you row numbers:

SELECT Address, City, State, Zip, 
ROW_NUMBER ( )    OVER ( [ PARTITION BY Address, City, State, Zip ORDER BY RecordingDate DESC) as RowNum
FROM MyTable

Open in new window


Now if there is an additional column, such as an identity column or a GUID it makes it easier to delete. But without that column it gets to be more difficult.
0
 
LVL 13

Expert Comment

by:magarity
Comment Utility
You can get the duplicates rather easily:
select count(1), address, city, state, zip from table group by  address, city, state, zip having count(1) > 1;
then you can delete them with whatever is the primary key. address_id or some such, I assume.
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 3

Expert Comment

by:smilieface
Comment Utility
Given that you don't care which of the duplicate records you get, you could use this.
SELECT
   Address,
   MAX(City)  AS City,
   MAX(State) AS State,
   MAX(Zip) AS Zip,
   MAX(RecordingDate) AS RecordingDate,
   MAX(DATEADD(dd, 7, RecordingDate)) AS LeadDate
   FROM <Table Name Here>
   GROUP BY Address

Open in new window

0
 
LVL 25

Expert Comment

by:jogos
Comment Utility
Took Jim .P query to start to find doubles everything with rownum=1 is unique or is an older duplicate.
So I introduced it in an understandable query that first select your results so you can check before you start to delete
select * from
--delete
 MyTable
where id in 
(select x.id 
 
from (
   SELECT id,Address, City, State, Zip, 
   ROW_NUMBER ( )    OVER ( PARTITION BY Address, City, State, Zip 
                                                ORDER BY RecordingDate   DESC) as RowNum
FROM MyTable
) as x
where x.RowNum > 1

Open in new window

0
 
LVL 42

Expert Comment

by:EugeneZ
Comment Utility
what is your sql server version?
0
 
LVL 42

Expert Comment

by:EugeneZ
Comment Utility
you may like to use this method to remove dups and prevent dups in future

How to remove duplicate rows from a table in SQL Server
http://support.microsoft.com/kb/139444
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
table fragmentation 40 73
t-sql complement 8 30
Copy Database Wizard Error 3 20
Azure SQL DB? 3 14
Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

744 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

19 Experts available now in Live!

Get 1:1 Help Now