Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

return row only if value not in other row

Posted on 2014-03-11
3
Medium Priority
?
52 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 1000 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 1000 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

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
In this blog post, we’ll look at how using thread_statistics can cause high memory usage.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

636 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