Solved

return row only if value not in other row

Posted on 2014-03-11
3
48 Views
Last Modified: 2016-06-04
Have the following subset of tblMember:

mem_num     product_type         address
1                          A                      main st
1                          C                      main st                      
2                          A                      1st street
2                          C                      1st street
2                          D                      1st street
3                          A                      2nd street
3                          B                      2nd street
4                          A                      3rd street
4                          C                      3rd street

I want to select only rows with product_type 'A', if mem_num does not have product type 'B' in another row or

mem_num       product_type         address
1                          A                         main
2                          A                         1st Street
4                          A                         3rdStreet

Thanks!
0
Comment
Question by:needplease
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 35

Accepted Solution

by:
Dan Craciun earned 250 total points
ID: 39919841
SELECT * FROM tblMember 
    WHERE product_type='A' AND mem_num NOT IN (
    SELECT mem_num FROM tblMember WHERE product_type='B'
)

Open in new window


HTH,
Dan
0
 
LVL 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 250 total points
ID: 39919901
I prefer for this kind of queries the NOT EXISTS syntax, especially if the number of rows having the product_type='B' here to be huge
SELECT m.*
  FROM tblMember  m
    WHERE m.product_type='A' 
AND NOT EXISTS( 
    SELECT null 
   FROM tblMember o
   WHERE o.product_type='B'
       and o.mem_num  = m.mem_num 
) 

Open in new window

you shall have a (single) index on the columns mem_num + product_type to ensure the query runs as fast as possible
0

Featured Post

Turn Insights into Action

Communication across every corner of your business is essential to increase the velocity of your application delivery and support pipeline. Automate, standardize, and contextualize your communication processes with xMatters.

Question has a verified solution.

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

Foreword In the years since this article was written, numerous hacking attacks have targeted password-protected web sites.  The storage of client passwords has become a subject of much discussion, some of it useful and some of it misguided.  Of cou…
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL several years ago, it seemed like now was a good time to update it for object-oriented PHP.  This article does that, replacing as much as possible the pr…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

696 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