?
Solved

SQL Server Select Query to exclude records based on values from lookup file.

Posted on 2014-01-21
2
Medium Priority
?
304 Views
Last Modified: 2014-02-01
This should be a pretty easy one.  I just need a SQL Select query that will exclude records based on a value from a look up exclusion file.    There are two files; the primary file called addresses and the lookup file called "Keywords" which contains one column called keywordvalue.  If the OWNER column from the "Addresses" file contains a value from the  keyword file it needs to be removed.  Ideally, I would like the removed records to be put into a separate file but I am not sure if that is possible.  

Addresses File:                                     Keyword File:
    Address                                             KeywordValue
    City      
    State
    Zip
    RecordingDate
    OWNER

Keyword File Example:
    Bank
    Home
    Business
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
2 Comments
 
LVL 34

Accepted Solution

by:
Brian Crowe earned 2000 total points
ID: 39797289
I am assuming that "file" = "table".  Also is this the entirety of the schema?  Are there identity columns in these tables?

Be aware that this matches partial words in other words "Banker" in Address.OWNER would match against "Bank".  If this is not the way you want it to work than we will have to adjust the logic

INSERT INTO OtherTable
(
   Address,
   City      
    State
    Zip
    RecordingDate
    OWNER
)
SELECT Address,
   City      
    State
    Zip
    RecordingDate
    OWNER
FROM Address AS A
INNER JOIN Keyword AS K
   ON A.OWNER LIKE '%' + K.KeywordValue + '%'

DELETE FROM Address
FROM OtherTable AS OT
INNER JOIN Address AS A
   ON ???

--This is where the primary key for this table comes in.  What uniquely identifies an Address record?
0
 
LVL 32

Expert Comment

by:awking00
ID: 39800912
Are we talking files or tables? If tables,

create table removedrecords as
select owner from addresses
 intersect
 select keywordvalue from keyword

Note - the above would have to come first

To remove -
delete from addresses
where owner in
(select keywordvalue from keyword)
0

Featured Post

Get proactive database performance tuning online

At Percona’s web store you can order full Percona Database Performance Audit in minutes. Find out the health of your database, and how to improve it. Pay online with a credit card. Improve your database performance now!

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
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.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Suggested Courses

762 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