Solved

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

Posted on 2014-01-21
2
293 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 31

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
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 and will be exposed to the many uses the SELECT statement has.

708 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

13 Experts available now in Live!

Get 1:1 Help Now