Solved

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

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Loop to go backward 90 days 2 18
Attaching Database Failed ? 3 42
SQL view 2 27
ms sql + get number in list out of total 7 29
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
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…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how the fundamental information of how to create a table.

770 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