Solved

Compare and filter data in sql server table

Posted on 2013-06-17
7
301 Views
Last Modified: 2013-06-17
Hi,

I have this table in sql server with the name address looking like this:
firstname, string
lastname, string
address, string
zip, string,
city, string

The table consists of data from different databases and I know there are duplicate addresses in there.

I would like to compare city, zip and lastname, so that those that have more than one instance in the table are left out (they are only shown one time).

How can I achieve this?

Thanks for help!

Peter
0
Comment
Question by:peternordberg
  • 3
  • 3
7 Comments
 
LVL 8

Expert Comment

by:Ganapathi
Comment Utility
Are you trying to filter them in your Select query or while inserting the table itself you want to ignore the duplicates?
0
 
LVL 48

Expert Comment

by:PortletPaul
Comment Utility
do you have a unique identifier for every row? (in addition to the columns already listed)

-- e.g. if one did this to locate the "duplicates"
select  city, zip,  lastname, count(*)
from YourTable
group by  city, zip,  lastname
having count(*) > 1

i.e. is there some existing field to help identify which of the duplicated records to retain?
0
 

Author Comment

by:peternordberg
Comment Utility
I'm going to filter in the select query.

Peter
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

Author Comment

by:peternordberg
Comment Utility
the unique identifier is customerID, int. I'm sorry I omitted that.

Peter
0
 
LVL 48

Expert Comment

by:PortletPaul
Comment Utility
>>I'm going to filter in the select query
is this something like what you are looking for?

select  city, zip,  lastname,  min(customerID) as customerID
from YourTable
group by  city, zip,  lastname

by the way, what happens if a family (same city, same zip, different homes) each become a customer?
0
 

Author Comment

by:peternordberg
Comment Utility
Thanks for answer. It's almost what I want.

Say that I would like to add firstname and address in my select statement, but I don't want to group by them since then the filter will also apply to those columns. How can I do that?

Peter
0
 
LVL 48

Accepted Solution

by:
PortletPaul earned 500 total points
Comment Utility
try this, it should be close
select
    customerID
  , city
  , zip
  , lastname
  , firstname
  , address
  from (
            select
                customerID
              , city
              , zip
              , lastname
              , firstname
              , address
              , row_number() over ( partition by city, zip, lastname 
                                    order by customerID
                                   ) as row_ref
            from YourTable
       ) as derived
where row_ref = 1

Open in new window

0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

771 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

10 Experts available now in Live!

Get 1:1 Help Now