473,407 Members | 2,629 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,407 software developers and data experts.

Problem with "Not In" Subquery

I have a problem with using a subquery in MS Access. The purpose of the sub-query is to create a list of people who have had doctor exams in the past 6 months and exclude them from the main query. This is what I came up with:

Expand|Select|Wrap|Line Numbers
  1. SELECT GI.generalinfoid AS GeneralInfoId, max(GI.FirstName) AS FirstName, max(GI.LastName) AS LastName, max(TI.ImagingDate) AS ImagingDate, Max(EM.embolizationDate) AS embolizationDate
  2.  
  3. FROM tblImaging AS TI RIGHT JOIN (qryGeneralInfo AS GI INNER JOIN tblEmbolizations AS EM ON GI.generalInfoID=EM.peopleID) ON TI.GeneralInfoID=GI.generalinfoid
  4.  
  5. WHERE ((GI.name) Is Not Null Or (GI.name)<>"" Or (GI.Lastname) Is Not Null Or (GI.Lastname)<>"") And (GI.generalinfoid)<>14598 And (GI.active) Like "Yes" And (GI.EMBCOUNT)>=1
  6.  
  7.  
  8. and TI.GeneralInfoID not in (select distinct GeneralInfoID from tblImaging TI where DateDiff("m",(TI.ImagingDate),Now())<=6)
  9.  
  10.  
  11. GROUP BY GI.generalinfoid
  12. ORDER BY max(GI.LastName);

The above query does not cause an error but it does not return any data. If I take out the subquery part, it returns over a thousand rows. If I replace "not in" with "in" in the subquery it returns about 65 rows. What can I do to recover the missing 900+ rows with the "not in" subquery?

Incidentally, I tried the following solution but it errored out my Access program:

Expand|Select|Wrap|Line Numbers
  1. SELECT GI.generalinfoid AS GeneralInfoId, max(GI.FirstName) AS FirstName, max(GI.LastName) AS LastName, max(TI.ImagingDate) AS ImagingDate, Max(EM.embolizationDate) AS embolizationDate
  2.  
  3.  
  4. FROM tblImaging AS TI RIGHT JOIN (qryGeneralInfo AS GI INNER JOIN tblEmbolizations AS EM ON GI.generalInfoID=EM.peopleID) ON TI.GeneralInfoID=GI.generalinfoid
  5.  
  6.  
  7. WHERE NOT EXISTS
  8.  
  9. (select distinct GeneralInfoID from tblImaging TI where TI.generalInfoID = GI.GeneralInfoID AND DateDiff("m",(TI.ImagingDate),Now())<=6)
  10.  
  11. AND
  12.  
  13. ((GI.name) Is Not Null Or (GI.name)<>"" Or (GI.Lastname) Is Not Null Or (GI.Lastname)<>"") And (GI.active) Like "Yes" And (GI.EMBCOUNT)>=1
  14.  
  15.  
  16. GROUP BY GI.generalinfoid
  17. ORDER BY max(GI.LastName);

Anyways, any help would be greatly appreciated. Thanks in advance.
May 23 '07 #1
13 3803
code green
1,726 Expert 1GB
How many rows does the subquery return?
Expand|Select|Wrap|Line Numbers
  1. select distinct GeneralInfoID from tblImaging TI where DateDiff("m",(TI.ImagingDate),Now())<=6
May 24 '07 #2
The subquery returns 65 Rows
May 24 '07 #3
Rabbit
12,516 Expert Mod 8TB
Try using a different alias for the subquery. Right now the main query's TI alias overlaps with the subquery's TI, that may be the problem. Or leave off the subquery's alias altogether.
May 24 '07 #4
Tried this...Gave me the same result of no rows

Expand|Select|Wrap|Line Numbers
  1. SELECT GI.generalinfoid AS GeneralInfoId, max(GI.FirstName) AS FirstName, max(GI.LastName) AS LastName, max(TI.ImagingDate) AS ImagingDate, Max(EM.embolizationDate) AS embolizationDate
  2.  
  3. FROM tblImaging AS TI RIGHT JOIN (qryGeneralInfo AS GI INNER JOIN tblEmbolizations AS EM ON GI.generalInfoID=EM.peopleID) ON TI.GeneralInfoID=GI.generalinfoid
  4.  
  5. WHERE ((GI.name) Is Not Null Or (GI.name)<>"" Or (GI.Lastname) Is Not Null Or (GI.Lastname)<>"") And (GI.generalinfoid)<>14598 And (GI.active) Like "Yes" And (GI.EMBCOUNT)>=1
  6.  
  7.  
  8. and TI.GeneralInfoID not in (select distinct GeneralInfoID from tblImaging tbm where DateDiff("m",(tbm.ImagingDate),Now())<=6)
  9.  
  10.  
  11. GROUP BY GI.generalinfoid
  12. ORDER BY max(GI.LastName);
May 25 '07 #5
NeoPa
32,556 Expert Mod 16PB
It seems to me that your original line 8
Expand|Select|Wrap|Line Numbers
  1. and TI.GeneralInfoID not in (select distinct GeneralInfoID from tblImaging TI where DateDiff("m",(TI.ImagingDate),Now())<=6)
is equivalent to :
Expand|Select|Wrap|Line Numbers
  1. AND DateAdd('m',6,TI.ImagingDate)>=Date()
Would that not be a little more straightforward?
May 25 '07 #6
Rabbit
12,516 Expert Mod 8TB
It seems to me that your original line 8
Expand|Select|Wrap|Line Numbers
  1. and TI.GeneralInfoID not in (select distinct GeneralInfoID from tblImaging TI where DateDiff("m",(TI.ImagingDate),Now())<=6)
is equivalent to :
Expand|Select|Wrap|Line Numbers
  1. AND DateAdd('m',6,TI.ImagingDate)>=Date()
Would that not be a little more straightforward?
Good catch, totally missed that. I should concern myself more with what the query is trying to achieve.

But a not on aliases. If you use an alias, then every field from that table has to be prefixed with alias.
May 25 '07 #7
NeoPa
32,556 Expert Mod 16PB
But a not on aliases. If you use an alias, then every field from that table has to be prefixed with alias.
Hi PW.
What did you mean by your second paragraph?
May 25 '07 #8
Rabbit
12,516 Expert Mod 8TB
Hi PW.
What did you mean by your second paragraph?
I don't know...

lol, What I meant was:

On a side note. If you use an alias, then every field from that table has to be prefixed with alias.
May 25 '07 #9
NeoPa
32,556 Expert Mod 16PB
A bit like the following? :
Expand|Select|Wrap|Line Numbers
  1. SELECT PW.Field1,PW.Field2
  2. FROM PeskyWabbit AS PW
Seriously, I didn't find that to be necessarily true in my testing.
Expand|Select|Wrap|Line Numbers
  1. SELECT PW.Field1,Field2
  2. FROM PeskyWabbit AS PW
worked fine for me (Strangely, I didn't use these exact terms :D).
May 25 '07 #10
Rabbit
12,516 Expert Mod 8TB
A bit like the following? :
Expand|Select|Wrap|Line Numbers
  1. SELECT PW.Field1,PW.Field2
  2. FROM PeskyWabbit AS PW
Yeah, he did that in his main query but not in his subquery.
May 25 '07 #11
NeoPa
32,556 Expert Mod 16PB
Sorry PW, I retro edited my last post to avoid accusations of post whoring :embarassed:

Seriously, I didn't find that to be necessarily true in my testing.

Expand|Select|Wrap|Line Numbers
  1. SELECT PW.Field1,Field2
  2. FROM PeskyWabbit AS PW
worked fine for me (Strangely, I didn't use these exact terms :D).
May 25 '07 #12
Rabbit
12,516 Expert Mod 8TB
Hmm, perhaps not. I don't know where I got the idea from.
May 25 '07 #13
NeoPa
32,556 Expert Mod 16PB
I would guess from one of those clever self-linking sub-queries you're so good at ;) There it would certainly make sense.
May 25 '07 #14

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

Similar topics

4
by: Marcin Dobrucki | last post by:
I've been having some problems with a parse error that I can't figure out (PHP 4.3.11 on Solaris9). Sample code: <?php // getting strange parse errors on this class A { var $value; function...
13
by: gary | last post by:
Hi, We all know the below codes are dangerous: { int *p = new int; delete p; delete p; } And we also know the compilers do not delete p if p==NULL. So why compilers do not "p = NULL"...
3
by: TCB | last post by:
Hi, There are a couple of things that are bothering me, any help on these is greatly appreciated. This is using ASP.NET 2.0 1. I am sending email in my web app, using a simple form here is the...
8
by: Chad | last post by:
Should links automatically appear in the "visited" color as defined by the user's IE settings without having to add any special coding? I have a situation where the link was not changing color...
3
by: Branco Medeiros | last post by:
Hi all, Using SQL Server 2000, SP4. I have a table of street names (Rua) whose ids (cod_rua) are foreign keys into a consumer table (Consumidor). It turns out that the "Rua" table has many...
9
by: Robbie Hatley | last post by:
Greetings, group. I just found a weird problem in a program where a variable declared in a {block} after a "case" keyword was being treated as having value 0 even though its actual value should...
1
by: anyonefour | last post by:
Hai How we can give the <a href> tag in xml code. Is ther any other tags for giving hyperlink in xml. Please inform me. Thanks & Regards
4
by: fran7 | last post by:
Hi, from help in the javascript forum I found the error in some code but need help. This bit of code works perfectly, trouble is I am writing it to a javascript function so the height needs to be in...
5
by: Muzammil | last post by:
i have problem with this operator "+" in doubly circular link list.(i think i have problem with return type). error is of instantiate error.(mean type dismatch) if any one can help me please...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.