Solved

Select from table A where in table B for one condition, but not in table B for another condition

Posted on 2011-02-24
2
315 Views
Last Modified: 2012-05-11
Hi All,

I've got a table that holds leads (id, firstname, surname etc)

Then I have another table which keeps a record of lead activity, i.e. when it's been viewed and assigned etc (id, person_id, lead_id, description). In this case, the description will hold the type of activity (viewed, assigned, deleted).

What I need to do is get a list of leads where they've been assigned, but not viewed. This would mean essential mean...
SELECT * FROM leads,lead_activity WHERE lead.id = lead_activity.lead_id AND lead_activity.description = 'assigned'

Somehow, in that statement I also need to do another query which checks to see if that lead is in the activity table again with the description of viewed, and if not, we need to include it in the results.

Does anyone have any ideas how to do this?
0
Comment
Question by:SheppardDigital
2 Comments
 
LVL 22

Accepted Solution

by:
pivar earned 500 total points
Comment Utility
Hi,

Try

select *
from leads
where exists (select 1 from lead_activity where leads.id = lead_activity.lead_id and lead_activity.description = 'assigned')
and not exists (select 1 from lead_activity where leads.id = lead_activity.lead_id and lead_activity.description = 'viewed')

/peter
0
 
LVL 3

Expert Comment

by:LFLFM
Comment Utility
If you just need the list of leads, I believe pivar's SQL is what you need. If you need to see the activities as well, then you just need to add the not exists clause to the sql you already had:
SELECT * FROM LEADS,LEAD_ACTIVITY
WHERE LEAD.ID = LEAD_ACTIVITY.LEAD_ID
AND LEAD_ACTIVITY.DESCRIPTION = 'assigned'
AND NOT EXISTS (SELECT 1 FROM LEAD_ACTIVITY
                WHERE LEADS.ID = LEAD_ACTIVITY.LEAD_ID
                AND LEAD_ACTIVITY.DESCRIPTION = 'viewed')

Open in new window

0

Featured Post

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

Join & Write a Comment

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

743 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

16 Experts available now in Live!

Get 1:1 Help Now