Results 1 to 4 of 4
  1. #1
    hillvalley45 is offline Novice
    Windows 7 32bit Access 2013 32bit
    Join Date
    Apr 2020
    Posts
    2

    Group records only if condition is true

    Hi guys, i really need your help to try and solve this problem
    I need to group the following records, to sum the amount but only when for the same Client in the same date with the same Purchase date, the OrderPaid is Yes in all records


    Reference Date Cur Cliente PurchaseDate Amount OrderPaid
    12345678 06-06-20 EUR AAAAA 30-01-20 100 Yes
    89123455 06-06-20 EUR AAAAA 30-01-20 10 Yes
    14635677 28-04-20 EUR BBBBB 20-04-20 5 Yes
    75688946 28-04-20 EUR BBBBB 20-04-20 90 No
    The result i´m trying to achive is:


    |06-06-2020|EUR|AAAAA|30-01-2020 |110|

    The other records will only be grouped when both orders have Yes or OrderPaid
    Many thanks in advance!

  2. #2
    A S MANN is offline Advanced System Analyst
    Windows XP Access 2007
    Join Date
    Oct 2010
    Location
    India
    Posts
    164
    Create a Aggregate Query with Cliente Group By , Date Group By , OrderPaid Condition "Yes", Amount As Sum

  3. #3
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,936
    don't think that will work - client BBBBB yes will be included, think the OP wants to exclude it because the other BBBBB record is No

    suggest you need two queries, one for yes, one for no, then left join the yeses to the nos and only include where the nos is null
    Code:
    SELECT yeses.*
    FROM 
        (SELECT Date, Cliente, PurchaseDate, sum(Amount) as Amt WHERE OrderPaid='Yes' GROUP BY  Date, Cliente, PurchaseDate) AS Yeses
        LEFT JOIN  
        (SELECT Date, Cliente, PurchaseDate, sum(Amount) as Amt WHERE OrderPaid='No' GROUP BY  Date, Cliente, PurchaseDate) AS Noes
        ON Yeses.Date = Noes.Date AND Yeses.Cliente = Noes.Cliente AND Yeses.PurchaseDate=Noes.PurchaseDate
    WHERE Noes.Date IS Null
    There may be more efficient ways of doing the Noes, but you get the idea

    perhaps this would suffice

    (SELECT Date, Cliente, PurchaseDate WHERE OrderPaid='No') AS Noes

  4. #4
    hillvalley45 is offline Novice
    Windows 7 32bit Access 2013 32bit
    Join Date
    Apr 2020
    Posts
    2
    Many thanks to you both for your prompt reply!
    A S MANN unfortunately your suggestion does not work because it will include other orders that have yes and no.

    Ajax your suggestion solves the problem!!!

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

Similar Threads

  1. Display field if condition is true
    By Ganga983 in forum Queries
    Replies: 1
    Last Post: 03-23-2019, 12:17 PM
  2. Change result if condition is true
    By rfigueroa1976 in forum Access
    Replies: 9
    Last Post: 09-07-2018, 01:52 PM
  3. Open another Form if condition is true
    By sjacksontx in forum Forms
    Replies: 3
    Last Post: 03-27-2015, 01:24 PM
  4. Replies: 1
    Last Post: 10-25-2011, 09:47 PM
  5. Start and Stop times where condition true
    By cheshire_smile in forum Queries
    Replies: 3
    Last Post: 07-05-2011, 09:59 PM

Tags for this Thread

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