?
Solved

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

Posted on 2014-01-20
7
Medium Priority
?
555 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 3

Assisted Solution

by:Sreeram
Sreeram earned 1000 total points
ID: 39795945
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 1000 total points
ID: 39796000
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
ID: 39796014
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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 3

Expert Comment

by:smilieface
ID: 39796083
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
ID: 39796174
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 43

Expert Comment

by:Eugene Z
ID: 39796396
what is your sql server version?
0
 
LVL 43

Expert Comment

by:Eugene Z
ID: 39796456
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

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

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

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.
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how the fundamental information of how to create a table.
Suggested Courses

752 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