Solved

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

Posted on 2014-01-21
2
297 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
2 Comments
 
LVL 34

Accepted Solution

by:
Brian Crowe earned 500 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
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

765 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