Solved

T-SQL: Rookie Question on Best Approach for Selecting

Posted on 2011-09-20
7
190 Views
Last Modified: 2012-05-12
I'm looking for the best approach on identifying, then deleting, some customer data.  

There's tables for basic customer info, telephone/email info, and marketing info.  Some customers have email info, some don't.  The problem I'm trying to deal with is identifying those customers who don't an email address, yet have asked us to email them marketing info.  

My select statement is:
SELECT entity.entity_id,  phone.phone_no, phone.phone_type, attr_ent.attr_val
FROM   (entity entity INNER JOIN attr_ent attr_ent ON (entity.entity_id=attr_ent.entity_id) AND (entity.co_cd=attr_ent.co_cd))
LEFT OUTER JOIN phone phone ON entity.phone_id=phone.phone_id
WHERE  attr_ent.attr_val LIKE 'e-%'

It gets me data like:
00001003        NULL                           NULL      E-Newsletter        
00001003        NULL                           NULL      E-Training Updates  
00001035        952-123-4567      Direct      E-Newsletter        
00001035        john.doe@acme.com      Email      E-Newsletter        
00001035        952-123-4567      Direct      E-Training Updates  
00001035        john.doe@acme.com      Email      E-Training Updates  
00001240        507-321-9876      Fax      E-Newsletter        
00001240        507-321-9876      Fax      E-Training Updates  

Customer numbers 00001003 and 00001240 should have their requesst for marketing info deleted, as there's no where to send it.  However, customer 00001035, has an email address and should get the marketing info.

I'm getting stuck doing the deletes on customers like 00001035 because I can't figure out how to identify them as valid requests.
0
Comment
Question by:anordquist
  • 5
7 Comments
 
LVL 18

Expert Comment

by:deighton
Comment Utility
could you do something like

DELETE entity WHERE entity.entity_id IN(
SELECT entity.entity_id
FROM   (entity entity INNER JOIN attr_ent attr_ent ON (entity.entity_id=attr_ent.entity_id) AND (entity.co_cd=attr_ent.co_cd))
LEFT OUTER JOIN phone phone ON entity.phone_id=phone.phone_id
WHERE  attr_ent.attr_val LIKE 'e-%'  AND phone.phone_no IS NULL AND phone.phone_type IS NULL)

0
 

Author Comment

by:anordquist
Comment Utility
The marketing info is in the attribute table.  There's one row per customer per marketing info type.  I  can delete the row safely if the phone.phone_type is NULL, but what happens when the check comes up with a phone.phone_type equal to "Phone" rather than to "E-Mail".  The attribute table row gets deleted, even if there might be a later record in the phone table for that customer that has the email info.

These aren't the exact names for the columns
               customer table
                  entity_id
                  attribute_id
0
 

Author Comment

by:anordquist
Comment Utility
The prior comment got away from me.....

Customer table
     entity_id
     attribute_id
     phone_id

Phone table      (used for phone, fax, website, email, etc.)
     phone_id
     phone_number
     phone_type

Attribute-customer table
    customer_id
    attribute_id

Attribute table
    attribute_id
    attribute_value
    attribute_type

So, if a customer doesn't have an email address, yet has requested marketing info, I want to delete the corresponding row in the attribute-customer table.  I need to be able to check all of the rows in the phone table for the customer before I can determine if a deletion is warranted.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 21

Expert Comment

by:Alpesh Patel
Comment Utility
Use Merge statement of SQL Server 2008 R2.

Merge Souce
Target
On

When Match
{;;;}
When not MAtche
{;;;}
0
 

Author Comment

by:anordquist
Comment Utility
I'm running SQL Server 2005.
0
 

Accepted Solution

by:
anordquist earned 0 total points
Comment Utility
Delete question, no solutions are forthcoming.
0
 

Author Closing Comment

by:anordquist
Comment Utility
Delete the question, no answers for SQL Server 2005.
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

Suggested Solutions

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how the fundamental information of how to create a table.

728 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

12 Experts available now in Live!

Get 1:1 Help Now