Results 1 to 7 of 7
  1. #1
    GeorgeB is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Jan 2012
    Posts
    18

    Question Filtering query based on multiple rows values

    I think it probably simple but I can't think out of the box to find the solution




    the data below is a sample of the table I have,

    Contract Number Section Number Order Number Order Status
    20000 300 1 Withdrawn
    20000 300 2 Withdrawn
    20000 300 3 Completed
    20000 300 4 Completed
    20000 300 5 Approved
    20000 300 6 Completed
    20000 566 1 Approved
    20000 322 1 Approved
















    what I am trying to achieve is extracting only the "section Number"s where all the "order status" are "completed" otherwise I don't want to know about it.

    I tried group by "Completed" it didn't work

    Thanks in advance

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    Maybe just:

    SELECT * FROM tablename WHERE [Order Status] = "Completed";

    or

    SELECT DISTINCT [Section Number] FROM tablename WHERE [Order Status] = "Completed";

    or

    SELECT [Section Number] FROM tablename WHERE [Order Status] = "Completed" GROUP BY [Section Number];
    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
    GeorgeB is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Jan 2012
    Posts
    18
    Thanks June7 for your reply,
    I tried all the three steps however it sill doesn't work.

    For Eg: "Section Number" 300 still appear will not all the order status completed.

    to elaborate in the example Section number 300 contains 6 orders.
    I only want it to appear in my query if all the order from 1:6 have the field of order status completed

    Once again thank you

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    That means record selection is dependent on values in other records of the same table. That requires subquery or domain aggregate function (DLookup, DCount, etc).

    Are section numbers used with multiple contracts? If not, try:

    SELECT * FROM table WHERE SectionNumber Not In (SELECT DISTINCT SectionNumber FROM table WHERE OrderStatus <> "Completed");
    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
    GeorgeB is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Jan 2012
    Posts
    18
    Thanks once again June7,
    the Section Numbers are unique,

    I tried the code you gave me but it causes Access to crash every time (the database got 85000+ row)
    any thoughts

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    I tested a similar query on a table of 67,000 records. The query opens but doesn't want to go to last record. It's just way too slow. When I changed the criteria so there were fewer records returned, it worked.

    However, a table of 19,000 records works just fine.

    Seems Access is choking on the Not qualifier. Alternatives:

    1. Build an aggregate query

    SELECT SectionNumber, Sum(IIf(OrderStatus<>"Completed",1,0)) FROM table AS CountNotComp GROUP BY SectionNumber;

    Then join that query to the table and apply filter criteria of CountNotComp Is 0

    SELECT table.*, CountNotComp
    FROM Query1 INNER JOIN table ON Query1.SectionNumber = table.SectionNumber
    WHERE (((Query1.CountNotComp)=0));

    2. Try domain aggregate:

    SELECT * FROM table WHERE DCount("OrderStatus","table","OrderStatus<>'Comple ted' AND SectionNumber='" & [SectionNumber] & "'") = 0;
    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.

  7. #7
    Dal Jeanis is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2013
    Location
    Dallas TX
    Posts
    1,742
    Hmmmm. You want only the section numbers where every Order Status = Complete"
    Code:
    SELECT
       Q1.Cont,
       Q1.Sect,
       T1.[Order Number] AS OrdNo,
       T1.[Order Status] As OrdStat
    FROM 
       (SELECT
          T1.[Contract Number] As Cont,
          T1.[Section Number] As Sect,
          Sum(1) AS NbrOrders,
          Sum(IIF(T1.[Order Status] ="Completed",1,0) AS NbrCompleted
       From 
          MyTable AS T1
       GROUP BY
          T1.[Contract Number] As Cont,
          T1.[Section Number] As Sect
       ) AS Q1
       INNER JOIN 
       MyTable AS T2
       ON  T2.[Contract Number] = Q1.Cont
       AND T2.[Section Number] = Q1.Sect
    WHERE Q1.NbrOrders = Q1.NbrCompleted;
    Of course, replace MyTable with your table name, and fix any other table name issues you find.

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

Similar Threads

  1. Replies: 7
    Last Post: 07-11-2013, 10:45 AM
  2. Replies: 1
    Last Post: 10-29-2012, 08:15 AM
  3. Replies: 2
    Last Post: 05-16-2012, 04:45 PM
  4. Help : Query based on data from multiple rows
    By GeorgeB in forum Queries
    Replies: 7
    Last Post: 03-28-2012, 11:27 PM
  5. Replies: 2
    Last Post: 08-28-2011, 06:06 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