If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > UNION in subqueries

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-26-04, 11:52
jwab jwab is offline
Registered User
 
Join Date: Jul 2004
Location: UK
Posts: 43
UNION in subqueries

I've done this query
Code:
SELECT SO.IMEI
FROM [STOCK OUT] SO
WHERE SO.IMEI NOT IN
 ((SELECT DISTINCT UCS.IMEI
   FROM
    (SELECT DISTINCT CS.IMEI
     FROM [CONNECTED STOCK] CS) 
     UNION
    (SELECT OC.IMEI
     FROM  [OLD CONNECTED STOCK] OC) UCS)
AND DEALER = 'CARMARTHEN'
Just to explain, I need to find all the IMEI's located in CARMARTHEN which are in the STOCK OUT table but are not in the tables CONNECTED STOCK or OLD CONNECTED STOCK.

First of all Access doesnt like it. It says UNION not allowed in subqueries.. which I though was bollocks as I'm sure it is. Perhaps I've done something very wrong I cant see.

Any aid?
Reply With Quote
  #2 (permalink)  
Old 08-26-04, 12:11
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,605
I'd suggest using a slightly different approach, something like:
Code:
SELECT SO.IMEI
   FROM [STOCK OUT] AS SO
   WHERE 'CARMARTHEN' = DEALER
      AND NOT EXISTS (SELECT *
         FROM  [CONNECTED STOCK] AS cs
         WHERE  cs.IMEI = so.IMEI)
      AND NOT EXISTS (SELECT * 
         FROM  [OLD CONNECTED STOCK] AS OC
         WHERE oc.IMEI = so.IMEI)
This works on every database engine that supports subqueries that I know of, and can be easily converted to LEFT JOIN syntax for the few engines that don't support sub-queries.

-PatP
Reply With Quote
  #3 (permalink)  
Old 08-26-04, 13:55
LKBrwn_DBA LKBrwn_DBA is offline
Registered User
 
Join Date: Jun 2003
Location: West Palm Beach, FL
Posts: 2,455
Cool

Do you think this will work?
Code:
SELECT SO.IMEI
  FROM [STOCK OUT]
 WHERE DEALER = 'CARMARTHEN'
MINUS
SELECT DISTINCT UCS.IMEI
  FROM [CONNECTED STOCK]
MINUS
SELECT OC.IMEI
  FROM [OLD CONNECTED STOCK]
__________________
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
Reply With Quote
  #4 (permalink)  
Old 08-26-04, 14:53
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,605
I'd be pretty surprised if it worked in MS-Access, although it seems likely that it might work in Oracle!

-PatP
Reply With Quote
  #5 (permalink)  
Old 08-27-04, 07:51
jwab jwab is offline
Registered User
 
Join Date: Jul 2004
Location: UK
Posts: 43
Thanks Pat, works well. Fast to.
I tried yours LKBrwn_DBA but it throws syntax errors, I tried to address them but it still wont be accepted

Code:
SELECT SO.IMEI
  FROM [STOCK OUT] AS SO
 WHERE DEALER = 'CARMARTHEN'
MINUS
(SELECT DISTINCT UCS.IMEI
  FROM [CONNECTED STOCK] AS UCS)
MINUS
(SELECT OC.IMEI
  FROM [OLD CONNECTED STOCK] AS OC)
Reply With Quote
  #6 (permalink)  
Old 08-27-04, 08:53
LKBrwn_DBA LKBrwn_DBA is offline
Registered User
 
Join Date: Jun 2003
Location: West Palm Beach, FL
Posts: 2,455
Maybe if you try it WITHOUT the parentesis it will work!
__________________
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On