473,804 Members | 4,288 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem with "Not In" Subquery

3 New Member
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 3833
code green
1,726 Recognized Expert Top Contributor
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
ThePrinceIsRight
3 New Member
The subquery returns 65 Rows
May 24 '07 #3
Rabbit
12,516 Recognized Expert Moderator MVP
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
ThePrinceIsRight
3 New Member
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,579 Recognized Expert Moderator MVP
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 Recognized Expert Moderator MVP
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,579 Recognized Expert Moderator MVP
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 Recognized Expert Moderator MVP
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,579 Recognized Expert Moderator MVP
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

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

Similar topics

4
2689
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 A() { $this->value = 1; }
13
3117
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" automatically after programs do "delete p"?
3
1762
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 code: Dim MyMailFrom As New Net.Mail.MailAddress("portal@xxx.com", "Portal XX.com") Dim MyMailTo As New Net.Mail.MailAddress("info@xxx.com")
8
2927
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 until I added the following style: .vl:visited{color:#808080;} And then referenced this style in my Anchor:
3
2124
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 unused records which I'd like to wipe out. For instance, there are some 2800 unused records in the "Rua" table, and only some 200 records actually being used by the "Consumidor" table (which, itself, has some 5000 records).
9
2116
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 have been something else. An extremely stripped-down version: int Function (int something) { switch(something) { case WHATEVER:
1
1062
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
3902
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 &quot;&quot; instead of "" otherwise I get an error message. Can anyone suggest how to write it so that it writes &quot; instead of "". I have tried all combinations of adding &quot; to the code but as soon as I think I am there I get throw out again. if...
5
1429
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 reply. template <class T> DoublyCircularLinkList<TDoublyCircularLinkList<T>:: operator + (const DoublyCircularLinkList& rhs) { if (head==0 && rhs.head==0) {
0
9589
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10593
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10340
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10085
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7626
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6858
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5663
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4304
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3000
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.