Results 1 to 5 of 5
  1. #1
    willkr is offline Competent Performer
    Windows 10 Access 2019
    Join Date
    Mar 2023
    Posts
    110

    Complex SELECT Statement

    I'm still working on an old app. I have a select statement to find the total of all credit card payments for all credit cards. The statement that I currently have is:



    SELECT DISTINCTROW Sum(tblCreditCardPayment.[CP Amount]) AS CCPmt, tblCreditCardPayment.[CC Account]
    FROM tblCreditCardPayment
    GROUP BY tblCreditCardPayment.[CC Account];

    This works fine, but I now want to find the total of all "active" credit cards. The active flag is in tblCreditAccounts.CCActive.

    Any ideas on how I can accomplish this?

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    What are the primary and foreign key linking fields that would join these tables?

    SELECT Sum([CP Amount]) AS CCPmt, Sum(IIf(CCActive, [CP Amount], 0)) AS CCPmtAct, tblCreditCardPayment.[CC Account]

    FROM tblCreditCardPayment
    INNER JOIN tblCreditAccounts
    ON tblCreditCardPayment.[CC Account] = tblCreditAccounts.[CC Account]
    GROUP BY tblCreditCardPayment.[CC Account];


    DISTINCTROW (or DISTINCT) should not be needed with GROUP BY - it's redundant
    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
    Join Date
    Apr 2017
    Posts
    1,680
    Junes query will return totals for all credit cards, but in field CCPmtAct leaves out payments, which are done after the card was deactivated.

    To get totals of currently active cards only:
    Code:
    SELECT Sum(ccp.[CP Amount]) AS CCPmt, ccp.[CC Account]
    FROM tblCreditCardPayment ccp
    INNER JOIN tblCreditAccounts cca
    ON ccp.[CC Account] = cca.[CC Account]
    WHERE cca.CCActive = True
    GROUP BY ccp.[CC Account];
    

  4. #4
    willkr is offline Competent Performer
    Windows 10 Access 2019
    Join Date
    Mar 2023
    Posts
    110
    Arvil,
    I"m not sure what you mean by ccp and cca. I think you are referring to the tables. So, I think that when it is expanded out it looks like this:

    SELECT Sum(tblCreditCardPayment.[CP Amount]) AS CCPmt, tblCreditCardPayment.[CC Account] FROM tblCreditCardPayment INNER JOIN tblCreditAccounts ON tblCreditCardPayment.[CC Account] = tblCreditAccounts.[CC Account] WHERE tblCreditAccounts.CCActive = True GROUP BY tblCreditAccounts.[CC Account];

    SELECT Sum(tblCreditCardPayment.[CP Amount]) AS CCPmt, tblCreditCardPayment.[CC Account] FROM tblCreditCardPayment INNER JOIN tblCreditAccounts ON tblCreditCardPayment.[CC Account] = tblCreditAccounts.[CC Account] WHERE tblCreditAccounts.CCActive = True GROUP BY tblCreditAccounts.[CC Account];

    Is this what you mean?

  5. #5
    Join Date
    Apr 2017
    Posts
    1,680
    In FROM clause, when declaring source tables, you can declare also aliases for them, and then use them to refer the tables through whole query.
    The reasons to do this:
    1. When using aliases shorter than the original file name, querystring will be more compact, and easier to read;
    2. In case different tables contain fields having same names, Access don't need to decide, from which one to read the value (and probably to get wrong one);
    3. In case you link the same table several times into query, using aliases is mandatory;
    4. In case you have some result field of query calculated as (Select ...), and refer there to some table referred in FROM clause of main query too, using aliases is mandatory.

    And yes, this was what I ment!

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

Similar Threads

  1. Complex IIf statement?
    By aellistechsupport in forum Programming
    Replies: 2
    Last Post: 04-21-2014, 05:06 PM
  2. Replies: 7
    Last Post: 10-30-2012, 11:22 AM
  3. IIF statement too complex, table instead?
    By Lauren1989 in forum Queries
    Replies: 3
    Last Post: 01-25-2012, 01:27 PM
  4. Complex IIF Statement
    By ksamaniego in forum Queries
    Replies: 3
    Last Post: 08-11-2011, 09:09 AM
  5. Replies: 2
    Last Post: 08-26-2009, 10:43 AM

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