472,980 Members | 1,767 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Null in subquery returns no records

This as the row source for a combo:

SELECT qryRole.RoleID, qryRole.Role
FROM qryRole
WHERE (((qryRole.RoleID) Not In (SELECT RoleID FROM qryRoleEvent INNER JOIN
qryEvent ON qryRoleEvent.EventID = qryEvent.EventID WHERE ProdID =
Forms!frmProductions!ProdID)))
ORDER BY qryRole.Role;

If there is just one RoleID with a null value in the subquery then the main
query returns no records atall.

This is needed:

SELECT qryRole.RoleID, qryRole.Role
FROM qryRole
WHERE (((qryRole.RoleID) Not In (SELECT RoleID FROM qryRoleEvent INNER JOIN
qryEvent ON qryRoleEvent.EventID = qryEvent.EventID WHERE ProdID =
Forms!frmProductions!ProdID AND RoleID IS NOT NULL)))
ORDER BY qryRole.Role;

Why?

Yours, Mike MacSween
Nov 12 '05 #1
5 4162
"Mike MacSween" <mi******************@btinternet.com> wrote in message news:<40**********************@news.aaisp.net.uk>. ..
This as the row source for a combo:

SELECT qryRole.RoleID, qryRole.Role
FROM qryRole
WHERE (((qryRole.RoleID) Not In (SELECT RoleID FROM qryRoleEvent INNER JOIN
qryEvent ON qryRoleEvent.EventID = qryEvent.EventID WHERE ProdID =
Forms!frmProductions!ProdID)))
ORDER BY qryRole.Role;

If there is just one RoleID with a null value in the subquery then the main
query returns no records atall.


That is the SQL standard, oddly IMHO.
DG
Nov 12 '05 #2
"Mike MacSween" <mi******************@btinternet.com> wrote in message news:<40**********************@news.aaisp.net.uk>. ..
This as the row source for a combo:

SELECT qryRole.RoleID, qryRole.Role
FROM qryRole
WHERE (((qryRole.RoleID) Not In (SELECT RoleID FROM qryRoleEvent INNER JOIN
qryEvent ON qryRoleEvent.EventID = qryEvent.EventID WHERE ProdID =
Forms!frmProductions!ProdID)))
ORDER BY qryRole.Role;

If there is just one RoleID with a null value in the subquery then the main
query returns no records atall.

This is needed:

SELECT qryRole.RoleID, qryRole.Role
FROM qryRole
WHERE (((qryRole.RoleID) Not In (SELECT RoleID FROM qryRoleEvent INNER JOIN
qryEvent ON qryRoleEvent.EventID = qryEvent.EventID WHERE ProdID =
Forms!frmProductions!ProdID AND RoleID IS NOT NULL)))
ORDER BY qryRole.Role;

Why?


Think of null as "has an unknown value". If one or more RoleId's are
unknown we can not for certain say that a specific roleid is not in
the set. Another way of thinking about it is to transform the inner
query as:

x not in (y1, y2, ..., null) ->
not (x=y1 or x=y2 or ... x=null) ->
x!=y1 and x!=y2 and ... x!=null ->
.... and null ->
null

null is problematic in sql, and there have been lots of discussions
about it. See for example:

http://www.firstsql.com/iexist2.htm

HTH
/Lennart
Nov 12 '05 #3
Thanks for that.

It's clear now. This http://www.firstsql.com/iexist3.htm is also
interesting. And the paragraph:

'Even when query developers carefully avoid any incorrect formulations, the
error may occur anyway. Many RDBMSs routinely perform transformations of
queries during their query optimization phases. For example, Date's original
query using IN could be transformed into the EXISTS query during
optimization. Normally, this is a correct transformation (Date used the
transformation since it is theoretically correct), but because of the error
in the definition of WHERE in ANSI SQL it changes a correct query into an
incorrect one. Even ostensibly correct queries can produce wrong results
when subqueries are used.'

is worrying.

New testing rule for Mike - always test with null values.

Yours, Mike MacSween
"Lennart Jonsson" <le*****@kommunicera.umea.se> wrote in message
news:6d**************************@posting.google.c om...
"Mike MacSween" <mi******************@btinternet.com> wrote in message

news:<40**********************@news.aaisp.net.uk>. ..
This as the row source for a combo:

SELECT qryRole.RoleID, qryRole.Role
FROM qryRole
WHERE (((qryRole.RoleID) Not In (SELECT RoleID FROM qryRoleEvent INNER JOIN qryEvent ON qryRoleEvent.EventID = qryEvent.EventID WHERE ProdID =
Forms!frmProductions!ProdID)))
ORDER BY qryRole.Role;

If there is just one RoleID with a null value in the subquery then the main query returns no records atall.

This is needed:

SELECT qryRole.RoleID, qryRole.Role
FROM qryRole
WHERE (((qryRole.RoleID) Not In (SELECT RoleID FROM qryRoleEvent INNER JOIN qryEvent ON qryRoleEvent.EventID = qryEvent.EventID WHERE ProdID =
Forms!frmProductions!ProdID AND RoleID IS NOT NULL)))
ORDER BY qryRole.Role;

Why?


Think of null as "has an unknown value". If one or more RoleId's are
unknown we can not for certain say that a specific roleid is not in
the set. Another way of thinking about it is to transform the inner
query as:

x not in (y1, y2, ..., null) ->
not (x=y1 or x=y2 or ... x=null) ->
x!=y1 and x!=y2 and ... x!=null ->
... and null ->
null

null is problematic in sql, and there have been lots of discussions
about it. See for example:

http://www.firstsql.com/iexist2.htm

HTH
/Lennart

Nov 12 '05 #4

"Lennart Jonsson" <le*****@kommunicera.umea.se> wrote in message >
x not in (y1, y2, ..., null) ->
not (x=y1 or x=y2 or ... x=null) ->
x!=y1 and x!=y2 and ... x!=null ->
... and null ->
null

Three valued logic is problematic, but I do not agree with the above.
Not all conditions containing a null evalutate to null, for example

False AND null evaluates to False

your line ... and null -> null does not 'respect' that rule.

For tree valued logic, the extra set above boolean logic is :

1. Not null --> null
2. false and null --> false
3. true and null --> null
4. false or null --> null
5. true or null --> true

If a complete condition evaluates to null in a 'complete' clause
it is handled as false, the 'concerning' row is not selected.

I know this is true for MS-SQL-server and for Oracle, I do not
know how this is implemented in other databases.

For Oracle (also MS-SQL-server if you create dual with one row).
null is generated by (null = null)
false and true are generated bij a simple compare.
0. select * from dual where (null = null)
1. select * from dual where not (null= null)
2. select * from dual where (1=0) and (null = null)
use select * from dual where not((1=0) and (null = null)) (Gives a line)
to see that 2 evaluates to false. not false --> true
3. select * from dual where (1=1) and (null = null)
4. select * from dual where (1=0) or (null = null)
5. select * from dual where (1=1) or (null = null) (Gives a line)

ben brugman
null is problematic in sql, and there have been lots of discussions
about it. See for example:

http://www.firstsql.com/iexist2.htm

HTH
/Lennart

Nov 12 '05 #5
"ben brugman" <be*@niethier.nl> wrote in message news:<40**********************@read.news.nl.uu.net >...
"Lennart Jonsson" <le*****@kommunicera.umea.se> wrote in message >
x not in (y1, y2, ..., null) ->
not (x=y1 or x=y2 or ... x=null) ->
x!=y1 and x!=y2 and ... x!=null ->
... and null ->
null


Three valued logic is problematic, but I do not agree with the above.
Not all conditions containing a null evalutate to null, for example

False AND null evaluates to False

your line ... and null -> null does not 'respect' that rule.


In general youre right, but in the context given x!=y[i] evaluates to
true. I was a bit sloppy, and left out the last steps of the
reduction. What I meant was:

x not in (y1, y2, ..., null) ->
not (x=y1 or x=y2 or ... x=null) ->
x!=y1 and x!=y2 and ... x!=null ->
true and true and ... and null ->
true and null ->
null

[...]

/Lennart
Nov 12 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: leegold2 | last post by:
I tried what's below, seemed OK, so I replaced an "IN" for the "=" in the subquery below because of the subquery's error message. I thought w/"IN" I'd get three (3) records returned as expected....
8
by: Andrew McNab | last post by:
Hi folks, I have a problem with an MS Access SQL query which is being used in an Access Report, and am wondering if anyone can help. Basically, my query (shown below) gets some records from a...
1
by: RiesbeckP | last post by:
Hi All, I have a DB where there are customer numbers and a few other fields. I want to be able to pull all of the null records for a particular field as well as all the other customer numbers...
6
by: plaster1 | last post by:
Been trying to come up with a query to filter-down my sample set into distinct records. For instance, lets say column1 is a sample set, column2 is the parameter, and column3 is a name and column4...
3
ADezii
by: ADezii | last post by:
Null as it relates to database development is one of life's little mysteries and a topic of total confusion for novices who venture out into the database world. A Null Value is not zero (0), a zero...
3
by: BurtonBach | last post by:
I previously wrote the following query which gives me that data I wanted and currently returns 436 records: SELECT qryTonnageSelect.Jobs_Job_ID, qryTonnageSelect.SumOfTransactions_TonsNetWeight,...
1
by: veaux | last post by:
Question deals with linking tables in queries. I'm not a code writer so use the GUI for all my queries. Table 1 - Master Table 2 - Sub1 Table 3 - Sub 2 All 3 tables have the same key field....
11
by: bikefixxer | last post by:
I'm a beginner with Access and am using the 2007 version on XP. I've created a database that keeps track of employee hours where I work. Everything has worked fine until we recently hired someone...
0
prabirchoudhury
by: prabirchoudhury | last post by:
CRITERIA; +-------------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------------+--------------+------+-----+---------+-------+...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.