473,405 Members | 2,185 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Select ContactID if field doesn't have a specific value

Lets say I have 2 tables:
"tblContactDetails" with fields "ContactID" and "UserName" and
"tblCommTrack" with fields "CommTrackID", "ContactID", "CommType", "DateContacted"
Case one: tblCommTrack has one row with values: 1,10,1, 2008-03-13 respectively. The following query works, because it doesn't return any rows for the indivudual with ContactID=10 WHERE CommType=1 AND DateContacted=2008-03-13
Expand|Select|Wrap|Line Numbers
  1.  SELECT DISTINCT(d.ContactID), d.UserName FROM tblContactDetails AS d Left Join tblCommTrack AS c ON d.ContactID = c.ContactID WHERE (c.CommTrackID IS NULL OR c.CommType=2 OR (c.DateContacted <> 2008-03-13) ORDER By d.UserName
Case two: tblCommTrack has two rows with values: 1,10,1, 2008-03-13 and 1,10,2, 2008-03-13 Now the query doesn't work, because a record for individual with ContactID=10 is returned, and I don't want that. I only want to return records for individuals who don't have a value of 1 in the CommType field AND if the record with CommType=1 has DateContacted=2008-03-13.
Mar 13 '08 #1
3 1454
Problem solved:
Expand|Select|Wrap|Line Numbers
  1. CREATE DEFINER=`root`@`localhost` PROCEDURE `getSpecNotContacted`(
  2. IN CoachID INT,
  3. IN iCommType INT,
  4. IN fromDate DATE,
  5. IN toDate DATE
  6. )
  7. BEGIN
  8. drop table IF EXISTS temp;
  9. CREATE TEMPORARY TABLE temp (CommTrackID INT, ContactID INT);
  10.  
  11.    INSERT INTO temp
  12.    SELECT CommTrackID, ContactID FROM tblCommTrack
  13.    WHERE DateContacted >= fromDate
  14.    AND DateContacted <= toDate
  15.    AND CommType=iCommType;
  16.  
  17.  
  18. SELECT DISTINCT(d.ContactID), d.UserName, d.TelCell
  19. FROM tblContactDetails AS d Left Join temp AS t ON d.ContactID = t.ContactID
  20. WHERE t.CommTrackID IS NULL
  21. AND d.CoachedByContactID = CoachID
  22. AND d.ContactID <> CoachID ORDER By d.UserName;
  23. END
Mar 14 '08 #2
ronverdonk
4,258 Expert 4TB
Congrats on finding the solution.

However, the moderators cannot continue to change your posts for enclosing code within code tags!!

So enclose any code within the proper code tags. See the Posting Guidelines on how to do that.

MODERATOR
Mar 14 '08 #3
Problem solved:
Expand|Select|Wrap|Line Numbers
  1. CREATE DEFINER=`root`@`localhost` PROCEDURE `getSpecNotContacted`(
  2. IN CoachID INT,
  3. IN iCommType INT,
  4. IN fromDate DATE,
  5. IN toDate DATE
  6. )
  7. BEGIN
  8. drop table IF EXISTS temp;
  9. CREATE TEMPORARY TABLE temp (CommTrackID INT, ContactID INT);
  10.  
  11.    INSERT INTO temp
  12.    SELECT CommTrackID, ContactID FROM tblCommTrack
  13.    WHERE DateContacted >= fromDate
  14.    AND DateContacted <= toDate
  15.    AND CommType=iCommType;
  16.  
  17.  
  18. SELECT DISTINCT(d.ContactID), d.UserName, d.TelCell
  19. FROM tblContactDetails AS d Left Join temp AS t ON d.ContactID = t.ContactID
  20. WHERE t.CommTrackID IS NULL
  21. AND d.CoachedByContactID = CoachID
  22. AND d.ContactID <> CoachID ORDER By d.UserName;
  23. END
This is what I came up with. Does anyone know a shorter way? (Thanks ronverdonk for helping me to post better.)
Mar 17 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Martin Feuersteiner | last post by:
Dear Group I wonder whether you can give me a syntax example for a SQL Statement. Lets assume I've a table containing three columns ContactID (Primary Key), Firstname and Lastname. I would...
3
by: Ian T | last post by:
Hi, I've got what I think (probably incorrectly) should be a simple SELECT : Two colums with data like col1 col2 1 50 1 51 2 50
12
by: Kevin Lyons | last post by:
Hello, I am trying to get my select options (courses) passed correctly from the following URL: http://www.dslextreme.com/users/kevinlyons/selectBoxes.html I am having difficulty getting the...
3
by: Tcs | last post by:
My backend is DB2 on our AS/400. While I do HAVE DB2 PE for my PC, I haven't loaded it yet. I'm still using MS Access. And no, I don't believe this is an Access question. (But who knows? I...
6
by: iam247 | last post by:
Hi I am a relative beginner with ASP and weak on syntax for sql statements. Basically I modify something which works. I have tblGroupContact with two fields both long integer - ContactID &...
0
by: iam247 | last post by:
Hi I'm using Access 2002. I have 2 tables tblGroupContact, tblGroupPermission, both have 2 fields identical structure: ContactID GroupID (Both are Composite keys and both hold integers) ...
4
by: deepee | last post by:
I have a drop down box in HTML using SELECT and OPTION tags: <select title="Choose a number" onchange="obscure()" name="Digit1" ID="Digit1"> <OPTION VALUE="">&nbsp;</OPTION> <OPTION...
0
by: mfh | last post by:
Hi, This is my first post. Thanks for all the information provided on the site. I read some posts and I didn't find exactly what I was looking for. I am creating a database to log calls Leaders...
2
by: printline | last post by:
Hi all I have a problem with a form script. The form script should do the following: When a user selects a value from a drop down list and hits the "next" button it should take him to a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.