Solved

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

Posted on 2014-01-20
7
535 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
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 250 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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 42

Expert Comment

by:EugeneZ
ID: 39796396
what is your sql server version?
0
 
LVL 42

Expert Comment

by:EugeneZ
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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

770 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