Results 1 to 3 of 3
  1. #1
    tricman10 is offline Novice
    Windows 10 Access 2016
    Join Date
    Mar 2018
    Posts
    2

    query with max date and grouping

    I have some accounts and payments based on a user. In a date range I need to sum the balance for the user. My problem is that when a user has made more than one payment in the date range, I need to get the MAX date of that. It always returns the MAX date, not the specified date.
    Example: If I have two payments, one was made on the 28th of February, and the other was made on 3rd of March, when I define the query to be less or equal to the first of March, it should return 28th of February, but because of grouping it returns 3rd of March.


    This is my query:

    Code:
    SELECT Z1.korisnik_id, Korisnik.imePrezime, Z1.datumRacuna, Z1.uDatum, Z1.dug, Z1.sumUplata, (Z1.dug-Z1.sumUplata) AS Balance, Z1.zgrada_id
    FROM (SELECT Z.korisnik_id, Z.sCena AS dug,SUM(UnosUplata.iznos) AS sumUplata,Z.datum AS datumRacuna, MAX(UnosUplata.datum) AS uDatum,Z.zgrada_id
    FROM (
    SELECT korisnik_id, datum, SUM(cena) AS sCena,zgrada_id
    FROM (
    SELECT korisnik_id,datum,cena, zgrada_id
    FROM Racun
    UNION
    SELECT korisnik_id,datum,cena, zgrada_id
    FROM RacunP
    ) AS a
    GROUP BY korisnik_id,datum, zgrada_id
    ) AS Z
    LEFT JOIN UnosUplata ON Z.korisnik_id = UnosUplata.korisnik_id
    WHERE Z.zgrada_id=1
    GROUP BY Z.korisnik_id, Z.datum, Z.sCena,Z.zgrada_id
    )  AS Z1 INNER JOIN Korisnik ON Z1.korisnik_id = Korisnik.ID
    WHERE (((Z1.uDatum)<"3/01/2018"))
    ORDER BY Z1.korisnik_id;


  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,549
    youd want a text box on the form to say, pick the month,
    txtMonthDate = 2/1/2018

    other text boxes to fill in the full month
    txtSTartDate = txtMonthDate
    txtEndDate = dateadd("d",-1,dateadd("m",1,txtStartDate))

    the query can now use this a limiter, (this is example sql, use the query designer for the correct one)

    select Max(Date), ClientID from table where [date] between forms!myform!txtStartDate and forms!myform!txtEndDate

  3. #3
    tricman10 is offline Novice
    Windows 10 Access 2016
    Join Date
    Mar 2018
    Posts
    2
    I have found the solution. My mistake was that I was putting the max date in the range.
    This is the solution for the query:
    Code:
    SELECT  Z1.korisnik_id, Korisnik.imePrezime, Z1.datumRacuna,Z1.uDatum, Z1.dug, Z1.sumUplata, (Z1.dug-Z1.sumUplata) AS Balance, Z1.zgrada_idFROM(
    SELECT Z.korisnik_id, Z.datum AS datumRacuna, Z.sCena AS dug, SUM(UnosUplata.iznos) as sumUplata,MAX(UnosUplata.datum) AS uDatum, Z.zgrada_id
    FROM (
    SELECT korisnik_id, datum, SUM(cena) AS sCena,zgrada_id
    FROM (
    SELECT korisnik_id,datum,cena, zgrada_id
    FROM Racun
    UNION
    SELECT korisnik_id,datum,cena, zgrada_id
    FROM RacunP
    ) AS a
    GROUP BY korisnik_id,datum, zgrada_id
    ) AS Z
    LEFT JOIN UnosUplata ON Z.korisnik_id = UnosUplata.korisnik_id
    WHERE z.zgrada_id=1 AND UnosUplata.datum <= '2/25/2018'
    GROUP BY Z.korisnik_id, Z.datum, Z.sCena,Z.zgrada_id
    )AS Z1
    INNER JOIN Korisnik ON Z1.korisnik_id = Korisnik.ID
    WHERE Z1.datumRacuna >= '2/2/2018'
    ORDER BY z1.korisnik_id

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

Similar Threads

  1. Need Query to stop grouping by Date!
    By Elwood07 in forum Queries
    Replies: 6
    Last Post: 07-10-2015, 01:27 PM
  2. Grouping By Week Using a Fixed Start Date
    By abinboston in forum Queries
    Replies: 1
    Last Post: 07-31-2014, 11:55 AM
  3. DCount with Date Grouping
    By brunor in forum Reports
    Replies: 2
    Last Post: 09-30-2011, 08:05 PM
  4. Replies: 5
    Last Post: 06-22-2011, 08:47 PM
  5. Replies: 9
    Last Post: 01-28-2011, 06:05 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