PC Review


Reply
Thread Tools Rate Thread

Confusing query results

 
 
Steven Sutton
Guest
Posts: n/a
 
      8th Feb 2008
The problem I am having is with an SQL statement I wrote in VBA but the
examples I am giving actually come from a Query where I could let Access
write the SQL statement for me.

I have a table that has 1143 records in it. If I run the following Query it
returns 11 records, which is correct:

SELECT tblJobTickets.JobNumber, tblJobs.intJobID, tblJobs.txtJobName,
tblJobs.Status, tblJobs.dtEntered, tblJobs.dtModified, tblJobs.txtUsername,
tblJobs.intStatusSort
FROM tblJobTickets RIGHT JOIN tblJobs ON tblJobTickets.intJobID =
tblJobs.intJobID
WHERE ((tblJobs.Status)<>'Completed');

If I run this Query, it returns 1142 records - also correct (at this time,
there is only one record with the Status of 'Canceled':

SELECT tblJobTickets.JobNumber, tblJobs.intJobID, tblJobs.txtJobName,
tblJobs.Status, tblJobs.dtEntered, tblJobs.dtModified, tblJobs.txtUsername,
tblJobs.intStatusSort
FROM tblJobTickets RIGHT JOIN tblJobs ON tblJobTickets.intJobID =
tblJobs.intJobID
WHERE ((tblJobs.Status)<>'Canceled');

But if I run this Query, I get all 1143 records and it seems to me that I
should be getting 10 - the 11 that aren't 'Completed' minus the one that is
'Canceled':

SELECT tblJobTickets.JobNumber, tblJobs.intJobID, tblJobs.txtJobName,
tblJobs.Status, tblJobs.dtEntered, tblJobs.dtModified, tblJobs.txtUsername,
tblJobs.intStatusSort
FROM tblJobTickets RIGHT JOIN tblJobs ON tblJobTickets.intJobID =
tblJobs.intJobID
WHERE (((tblJobs.Status)<>'Canceled')) OR (((tblJobs.Status)<>'Completed'));

Does anyone see why I am not getting the results I am expecting? If I
haven't made it clear, what I am wanting is to exclude all records whose
Status is either 'Completed' or 'Canceled' but instead I am getting all
records. Any suggestions? Thanks again to all who help.
 
Reply With Quote
 
 
 
 
Allen Browne
Guest
Posts: n/a
 
      9th Feb 2008
Not sure I followed you, Steven.

Your final query asks for the records WHERE
((tblJobs.Status <> 'Canceled') OR (tblJobs.Status <>'Completed'))

Only one part of an OR has to match to return the record.
The records where Status is Canceled match the first part.
The records where Status is Completed match the second part.
Hence all the Canceled and all the Completed get returned.
Is that what you intended?

On a different topic, you have tblJobs is on the outside of the join?
The criteria you applied have the effect of eliminiating nulls.
Hence the query will behave like an inner join.

--
Allen Browne - Microsoft MVP. Perth, Western Australia
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"Steven Sutton" <(E-Mail Removed)> wrote in message
news:BDFE8E6B-B8EF-4BFC-B324-(E-Mail Removed)...
> The problem I am having is with an SQL statement I wrote in VBA but the
> examples I am giving actually come from a Query where I could let Access
> write the SQL statement for me.
>
> I have a table that has 1143 records in it. If I run the following Query
> it
> returns 11 records, which is correct:
>
> SELECT tblJobTickets.JobNumber, tblJobs.intJobID, tblJobs.txtJobName,
> tblJobs.Status, tblJobs.dtEntered, tblJobs.dtModified,
> tblJobs.txtUsername,
> tblJobs.intStatusSort
> FROM tblJobTickets RIGHT JOIN tblJobs ON tblJobTickets.intJobID =
> tblJobs.intJobID
> WHERE ((tblJobs.Status)<>'Completed');
>
> If I run this Query, it returns 1142 records - also correct (at this time,
> there is only one record with the Status of 'Canceled':
>
> SELECT tblJobTickets.JobNumber, tblJobs.intJobID, tblJobs.txtJobName,
> tblJobs.Status, tblJobs.dtEntered, tblJobs.dtModified,
> tblJobs.txtUsername,
> tblJobs.intStatusSort
> FROM tblJobTickets RIGHT JOIN tblJobs ON tblJobTickets.intJobID =
> tblJobs.intJobID
> WHERE ((tblJobs.Status)<>'Canceled');
>
> But if I run this Query, I get all 1143 records and it seems to me that I
> should be getting 10 - the 11 that aren't 'Completed' minus the one that
> is
> 'Canceled':
>
> SELECT tblJobTickets.JobNumber, tblJobs.intJobID, tblJobs.txtJobName,
> tblJobs.Status, tblJobs.dtEntered, tblJobs.dtModified,
> tblJobs.txtUsername,
> tblJobs.intStatusSort
> FROM tblJobTickets RIGHT JOIN tblJobs ON tblJobTickets.intJobID =
> tblJobs.intJobID
> WHERE (((tblJobs.Status)<>'Canceled')) OR
> (((tblJobs.Status)<>'Completed'));
>
> Does anyone see why I am not getting the results I am expecting? If I
> haven't made it clear, what I am wanting is to exclude all records whose
> Status is either 'Completed' or 'Canceled' but instead I am getting all
> records. Any suggestions? Thanks again to all who help.


 
Reply With Quote
 
John Spencer
Guest
Posts: n/a
 
      9th Feb 2008
Try replacing the OR with AND.

Boolean logic can be confusing. You want records where Status is not
canceled and where status is not Completed.

WHERE (((tblJobs.Status)<>'Canceled')) AND ((tblJobs.Status)<>'Completed'))

Or you can use
WHERE tblJobs.Status Not IN('Canceled','Completed')

Or you can use

WHERE Not (tblJobs.Status ='Canceled' OR tblJobs.Status='Completed')


'====================================================
John Spencer
Access MVP 2002-2005, 2007-2008
Center for Health Program Development and Management
University of Maryland Baltimore County
'====================================================


Steven Sutton wrote:
> The problem I am having is with an SQL statement I wrote in VBA but the
> examples I am giving actually come from a Query where I could let Access
> write the SQL statement for me.
>
> I have a table that has 1143 records in it. If I run the following Query it
> returns 11 records, which is correct:
>
> SELECT tblJobTickets.JobNumber, tblJobs.intJobID, tblJobs.txtJobName,
> tblJobs.Status, tblJobs.dtEntered, tblJobs.dtModified, tblJobs.txtUsername,
> tblJobs.intStatusSort
> FROM tblJobTickets RIGHT JOIN tblJobs ON tblJobTickets.intJobID =
> tblJobs.intJobID
> WHERE ((tblJobs.Status)<>'Completed');
>
> If I run this Query, it returns 1142 records - also correct (at this time,
> there is only one record with the Status of 'Canceled':
>
> SELECT tblJobTickets.JobNumber, tblJobs.intJobID, tblJobs.txtJobName,
> tblJobs.Status, tblJobs.dtEntered, tblJobs.dtModified, tblJobs.txtUsername,
> tblJobs.intStatusSort
> FROM tblJobTickets RIGHT JOIN tblJobs ON tblJobTickets.intJobID =
> tblJobs.intJobID
> WHERE ((tblJobs.Status)<>'Canceled');
>
> But if I run this Query, I get all 1143 records and it seems to me that I
> should be getting 10 - the 11 that aren't 'Completed' minus the one that is
> 'Canceled':
>
> SELECT tblJobTickets.JobNumber, tblJobs.intJobID, tblJobs.txtJobName,
> tblJobs.Status, tblJobs.dtEntered, tblJobs.dtModified, tblJobs.txtUsername,
> tblJobs.intStatusSort
> FROM tblJobTickets RIGHT JOIN tblJobs ON tblJobTickets.intJobID =
> tblJobs.intJobID
> WHERE (((tblJobs.Status)<>'Canceled')) OR (((tblJobs.Status)<>'Completed'));
>
> Does anyone see why I am not getting the results I am expecting? If I
> haven't made it clear, what I am wanting is to exclude all records whose
> Status is either 'Completed' or 'Canceled' but instead I am getting all
> records. Any suggestions? Thanks again to all who help.

 
Reply With Quote
 
Steven Sutton
Guest
Posts: n/a
 
      11th Feb 2008
Thank you John. I tried changing the OR to AND not 5 minutes after I posted
this question and sure enough I got what I was wanting regards the results. I
tried posting back that I had found the answer I needed but I had all kinds
of trouble getting logged in to this site and was unable to add a new post
that I had found the answer. Thanks for your answer and I certainlt didn't
know that there were so many different ways to phrase the WHERE clause!

Allen - I just wish I knew enough about Access to be able to answer your
questions! To be perfectly honest, I tried different Joins until I finally
got the results I wanted without really knowing the differences from one Join
Type to the next. Someday I hope to understand what I have done! :-)

"John Spencer" wrote:

> Try replacing the OR with AND.
>
> Boolean logic can be confusing. You want records where Status is not
> canceled and where status is not Completed.
>
> WHERE (((tblJobs.Status)<>'Canceled')) AND ((tblJobs.Status)<>'Completed'))
>
> Or you can use
> WHERE tblJobs.Status Not IN('Canceled','Completed')
>
> Or you can use
>
> WHERE Not (tblJobs.Status ='Canceled' OR tblJobs.Status='Completed')
>
>
> '====================================================
> John Spencer
> Access MVP 2002-2005, 2007-2008
> Center for Health Program Development and Management
> University of Maryland Baltimore County
> '====================================================
>
>
> Steven Sutton wrote:
> > The problem I am having is with an SQL statement I wrote in VBA but the
> > examples I am giving actually come from a Query where I could let Access
> > write the SQL statement for me.
> >
> > I have a table that has 1143 records in it. If I run the following Query it
> > returns 11 records, which is correct:
> >
> > SELECT tblJobTickets.JobNumber, tblJobs.intJobID, tblJobs.txtJobName,
> > tblJobs.Status, tblJobs.dtEntered, tblJobs.dtModified, tblJobs.txtUsername,
> > tblJobs.intStatusSort
> > FROM tblJobTickets RIGHT JOIN tblJobs ON tblJobTickets.intJobID =
> > tblJobs.intJobID
> > WHERE ((tblJobs.Status)<>'Completed');
> >
> > If I run this Query, it returns 1142 records - also correct (at this time,
> > there is only one record with the Status of 'Canceled':
> >
> > SELECT tblJobTickets.JobNumber, tblJobs.intJobID, tblJobs.txtJobName,
> > tblJobs.Status, tblJobs.dtEntered, tblJobs.dtModified, tblJobs.txtUsername,
> > tblJobs.intStatusSort
> > FROM tblJobTickets RIGHT JOIN tblJobs ON tblJobTickets.intJobID =
> > tblJobs.intJobID
> > WHERE ((tblJobs.Status)<>'Canceled');
> >
> > But if I run this Query, I get all 1143 records and it seems to me that I
> > should be getting 10 - the 11 that aren't 'Completed' minus the one that is
> > 'Canceled':
> >
> > SELECT tblJobTickets.JobNumber, tblJobs.intJobID, tblJobs.txtJobName,
> > tblJobs.Status, tblJobs.dtEntered, tblJobs.dtModified, tblJobs.txtUsername,
> > tblJobs.intStatusSort
> > FROM tblJobTickets RIGHT JOIN tblJobs ON tblJobTickets.intJobID =
> > tblJobs.intJobID
> > WHERE (((tblJobs.Status)<>'Canceled')) OR (((tblJobs.Status)<>'Completed'));
> >
> > Does anyone see why I am not getting the results I am expecting? If I
> > haven't made it clear, what I am wanting is to exclude all records whose
> > Status is either 'Completed' or 'Canceled' but instead I am getting all
> > records. Any suggestions? Thanks again to all who help.

>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Confusing results Papa Jonah Microsoft Excel Worksheet Functions 4 5th Mar 2010 10:38 PM
SCANPST yields confusing results Dave Jenkins Microsoft Outlook Discussion 4 10th Dec 2008 03:06 AM
Confusing Query (Flat File Query/Report from Relational DB) =?Utf-8?B?UmljaGFyZA==?= Microsoft Access Queries 1 3rd Jan 2007 09:08 AM
Confusing scan results Sanjaya Anti-Virus 5 18th Mar 2006 05:53 PM
Confusing results with CustomValidator and Datagrid controls 2obvious Microsoft ASP .NET 2 3rd Jan 2005 04:56 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:14 AM.