Solved

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

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

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

Suggested Solutions

Title # Comments Views Activity
SQL Error in WHERE Clause 5 42
Sql Join Problem 2 33
Serach for record in Microsoft SQL Management Studio 8 21
Adding Subtracting values in sql server 5 20
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.
I have a large data set and a SSIS package. How can I load this file in multi threading?
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 INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

864 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

23 Experts available now in Live!

Get 1:1 Help Now