Page 1 of 3 123 LastLast
Results 1 to 15 of 33
  1. #1
    UTLee is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Aug 2013
    Posts
    73

    Query To Show First Date for all Clients

    Need some help with a query. I'm trying to show the first date for each client where beginning balance = 0, ending balance > 0. The query below works for the most part because almost all clients only have 1 record where balance = 0 and ending balance > 0. However, I noticed that one client has 2 records where this occurred and so I only want to show the earliest date in this situation.



    Here's what I have so far and I'm sure there is a much more efficient way of doing this but I have very limited SQL knowledge:


    Code:
    SELECT tblAccountHistory.ClientID, tblAccountHistory.TransDate, Sum(tblAccountHistory.BegBal) AS SumOfBegBal, Sum(tblAccountHistory.NetContributions) AS SumOfNetContributions, Sum(tblAccountHistory.NetInvestmentChange) AS SumOfNetInvestmentChange, Sum(tblAccountHistory.EndingBal) AS SumOfEndingBal
    FROM tblAccountHistory INNER JOIN tblClients ON tblAccountHistory.ClientID = tblClients.client
    GROUP BY tblAccountHistory.ClientID, tblAccountHistory.TransDate, tblClients.cycle
    HAVING (((Sum(tblAccountHistory.BegBal))=0) AND ((Sum(tblAccountHistory.EndingBal))>0) AND ((tblClients.cycle)=1));
    Thanks in advance

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,963
    Try the Min function.
    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
    UTLee is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Aug 2013
    Posts
    73
    The only way I know how to use the Min function is in query design. So I selected min under the TransDate field and it only showed 2 records (there should be 381)

  4. #4
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,963
    You did this in the GROUP BY query you posted?

    If you want to still group by TransDate, will have to apply Min function to a copy of the TransDate field in the query grid.
    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
    UTLee is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Aug 2013
    Posts
    73
    Yes, I did this to the query above in the design mode. I selected Min under the TransDate field but that didn't work. How do I "apply Min function to a copy of the TransDate field in the query grid"? Could you elaborate?

    Thanks

  6. #6
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,963
    Just drag the TransDate field into the grid again and use Min function on one and Group By on the other.
    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
    UTLee is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Aug 2013
    Posts
    73
    Ok, did that but it didn't change the results of the original query. Its like it didn't have any effect on the query. I still see 382 records and there should only be 381 (the same particular client is still showing up twice).

  8. #8
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,963
    That's because TransDate is included in the GROUP BY. But you said if it's not included in the GROUP BY there are only 2 records?
    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.

  9. #9
    UTLee is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Aug 2013
    Posts
    73
    yea, If I have only one TransDate field in the query and I select Min instead of Group By then I only get 2 records

  10. #10
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,963
    That's odd. There are only 2 clients in tblAccountHistory?
    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.

  11. #11
    UTLee is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Aug 2013
    Posts
    73
    no there are thousands of records in tblAccountHistory - it holds our clients' account history (beginning balance, end balance, etc on a monthly basis) for every account they have. So the query takes all of those monthly account intervals and sums them together at the client level so that I only see 1 aggregate interval per month for each client instead of seeing their individual account intervals.

  12. #12
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    If you're trying to group your data by month don't GROUP BY TRANSDATE, try creating a field that's looks at the month/year combination of the TRANSDATE like:

    GroupDate: Datepart("yyyy", Transdate) & right("0" & datepart("m", transdate), 2)

    this should give you 201401 for january of 2014

  13. #13
    UTLee is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Aug 2013
    Posts
    73
    So I removed the TransDate field from the query and added "GroupDate: Datepart("yyyy", Transdate) & right("0" & datepart("m", transdate), 2)" to the query using the builder feature and it displays what you said it would, YYYYMM, but I'm still getting the same results as before. I'm getting 382 records when I should only be getting 381 and the reason is because one particular client has two monthly intervals where BegBal = 0 and EndBal > 0. So I want the query to only display the earliest interval for each client where BegBal = 0 and EndBal > 0. The point of this query is to show each client's inception date and I'm defining inception as the first instance where BegBal = 0 and EndBal > 0.

    Thanks again for everyone's help.

  14. #14
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    how are you identifying the inception record, is that the CYCLE field? or are you doing it strictly based on date?

    if your GROUP BY clause is CLIENTID, the month calculation and the CYCLE the CYCLE must be the item that's forcing the second record. Are those cycles incremental? Can you reliably determine which is the 'first' record by either choosing the minimum or maximum value for a specific client in that field?

  15. #15
    UTLee is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Aug 2013
    Posts
    73
    Sorry, Cycle indicates whether the Client is a managed or non-managed client. Basically, the cycle field is either the number 1 or 2. 1 means this is a managed client, 2 means this is a non-managed client so I don't want the non-managed clients in the query. I'm trying to identify the inception date strictly based on date.

Page 1 of 3 123 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 12
    Last Post: 11-04-2013, 07:08 AM
  2. Query list of clients in date range
    By scorpiogray in forum Queries
    Replies: 2
    Last Post: 05-20-2013, 01:34 PM
  3. Replies: 9
    Last Post: 02-12-2013, 03:14 PM
  4. Query to seperate active/inactive clients
    By csnyder1582 in forum Queries
    Replies: 5
    Last Post: 05-05-2011, 12:01 PM
  5. query to show gaps in a date field
    By Lockrin in forum Database Design
    Replies: 1
    Last Post: 05-28-2010, 10:48 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