Results 1 to 9 of 9
  1. #1
    ramangill is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Apr 2014
    Posts
    5

    Struggling with this query

    Basically it is easy. I have about 20 records with 3 columns. ID, Username and TX. TX values are random characters. I need to write a query where TX is either one of 3 TX values and where TX is one of 5 values and where TX is one of 7 values. If neither of the groupings have a match then the query should return 0 rows. For some reason this simple logic always returns 0. I can not figure it out.



    tx values are

    a1
    a2
    a3
    b1
    b2
    b3
    b4
    etc, etc, etc.

    Any suggestions. I was doing something like:

    select * from abc
    where tx in ('a1,a2,a3) and tx in (b1,b2,b4) and etc, etc.

    Logically I am hoping it would bring the rows that match all of the conditions in which this case it should however it returns nothing.

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    The 'and' operator won't work because no record can match the criteria. Each record has only one TX value, how can any record match all 3 criteria?

    Perhaps you want the OR operator?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    ramangill is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Apr 2014
    Posts
    5
    But my goal is to meet the criteria in set 1 and criteria of set 2 and criteria of set 3. I understand what your saying and it makes sense. But how would I make this into SQL logic. If at least one of the criteria sets does not match then I want the overall query to return 0 rows. If at least one match is found in each criteria set then return those rows. Hope this clears it up and you can assist. Looking forward to the response.

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Did you try the OR operator instead AND? Still not what you want? You showed an example of raw data, now show what you want for output.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    ramangill is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Apr 2014
    Posts
    5
    Raw data:

    ID username SAPTX
    1 parm
    2 parm xk99
    3 parm
    4 parm me21n
    5 parm me22n
    6 parm me2l
    7 parm me59
    8 parm me29n
    9 parm miro
    10 parm f-43
    11 parm f110
    12 parm f-53
    13 parm fb60


    What I am trying to do is:

    SELECT * from
    tab
    where
    saptx in ('xk01','xk02','xk99')
    and
    saptx in ('me21n','me22n','me2l','me59','me29n')
    and
    saptx in ('miro','f-43')
    and
    saptx in ('f110','f-53','fb60')

    So the above query will return 11 rows.

    But if row ID 2 had a value of SAPTX = 'abc' then the above query should be giving me 0 rows. Basically I am trying to do a minus query like I would in Oracle and I was able to get it working in Oracle fine, but the requirement is MS Access and I am trying. I know now the query will not work, but I am trying get to the point where I can roll with it.
    Any ideas?

  6. #6
    ramangill is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Apr 2014
    Posts
    5
    I was working with this query as well:

    select * from test a
    where a.saptx in ('xk01','xk02','xk99')
    union
    select * from test b
    where b.saptx in ('me21n','me22n','me2l','me59','me29n')
    union
    select * from test c
    where c.saptx in ('miro','f-43')
    union
    select * from test d
    where d.saptx in ('f110','f-53','fb60')

    Trying to get this to some sort of a MINUS like where if one of the UNIONS returns 0 rows then the whole query should return 0 rows.
    Any thoughts?

  7. #7
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Selecting records based on values in other records of same table is tricky. Usually requires nested subquery or domain aggregate function calls. The latter can be very slow performance.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  8. #8
    ramangill is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Apr 2014
    Posts
    5
    Any ideas on how or a start?

  9. #9
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Consider:

    Query1
    SELECT tab.username, Sum(saptx In ('xk01','xk02','xk99')) AS a, Sum(saptx In ('me21n','me22n','me2l','me59','me29n')) AS b, Sum(saptx In ('miro','f-43')) AS c, Sum(saptx In ('f110','f-53','fb60')) AS d
    FROM tab
    WHERE ((Not (tab.[SAPTX]) Is Null))
    GROUP BY tab.username;

    Query2
    SELECT tab.ID, tab.username, tab.SAPTX, Query1.a, Query1.b, Query1.c, Query1.d
    FROM Query1 INNER JOIN tab ON Query1.username = tab.username
    WHERE (((Query1.a)<>0) AND ((Query1.b)<>0) AND ((Query1.c)<>0) AND ((Query1.d)<>0));
    Last edited by June7; 04-10-2014 at 11:22 PM.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. New to access and struggling
    By Erica Nichols in forum Reports
    Replies: 3
    Last Post: 03-28-2012, 03:02 PM
  2. Struggling with random ratings
    By EBDoom in forum Queries
    Replies: 5
    Last Post: 03-20-2012, 02:52 PM
  3. Still struggling with relating tables
    By djclntn in forum Database Design
    Replies: 31
    Last Post: 02-14-2012, 02:08 PM
  4. My first Database, struggling a bit
    By ravihotwok in forum Access
    Replies: 1
    Last Post: 12-07-2011, 05:17 AM
  5. struggling with query, plz help
    By jimgros in forum Queries
    Replies: 2
    Last Post: 07-26-2011, 03:35 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums