Solved

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

Posted on 2014-01-20
7
533 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
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.

 
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

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.

Question has a verified solution.

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

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
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.
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.

932 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