Create Query to evaluate Max Date recognizing Null as High Value

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • HoganGroup
    New Member
    • Oct 2007
    • 8

    Create Query to evaluate Max Date recognizing Null as High Value

    Hi Experts and Fellow Duffers:

    I am trying to create a query which will identify the most recent EndDate in tblSchedules associated with tblKids.KidID where a Null value (indicating a current schedule) is the most recent.

    Max(EndDate) doesn't recognize Null as the highest value. Last(EndDate) returns the most recently entered schedule but not the most recent EndDate.

    Any suggestions? My query so far:

    [CODE=sql]SELECT tblKids.KidID, Min(tblSchedule s.StartDate) AS OriginalStart, Max(tblSchedule s.EndDate) AS FinalEnd
    FROM tblKids INNER JOIN tblSchedules ON (tblKids.KidID = tblSchedules.Ki dID) AND (tblKids.KidID = tblSchedules.Ki dID) AND (tblKids.KidID = tblSchedules.Ki dID)
    GROUP BY tblKids.KidID;
    [/CODE]
    Thanks,
    Bradley
  • Scott Price
    Recognized Expert Top Contributor
    • Jul 2007
    • 1384

    #2
    Try ORDER BY tblKids.EndDate ...

    Welcome to the Scripts!

    Regards,
    Scott

    Comment

    • nico5038
      Recognized Expert Specialist
      • Nov 2006
      • 3080

      #3
      When you see Null as the highest enddate, you should use the NZ() function e.g. like:
      Code:
      Max(Nz(EndDate,#01-01-2999#))
      Getting the idea ?

      Nic;o)

      Comment

      • HoganGroup
        New Member
        • Oct 2007
        • 8

        #4
        Thank you both you both for you replies. Both the ORDER BY and Nz() suggestions were good but occurred after the query evaluated Max() so I was still left with the max non-null date (which disregards current schedules).

        After playing, I got to the following partial solution which uses iif (instead on Nz) to create a "FakeEnd" for Null rows and an additional field to put them back to Null (which would be the one field I would use.) Technically it works but prompts the user for a "FakeEnd" parameter before figuring it out which really isn't going to work practically.

        I have a thought that a subquery (to make FakeEnd) might be appropriate here but I've reached the limit of my expertise with regard to queries, let alone subqueries. If either/any of you think a subquery might work, what would it look like?

        [CODE=sql]SELECT tblKids.KidID, Min(tblSchedule s.StartDate) AS OriginalStart, Max(IIf(IsNull([tblSchedules].[EndDate]),#12/31/9999#,[tblSchedules].[EndDate])) AS FakeEnd, IIf([FakeEnd]=#12/31/9999#,Null,[FakeEnd]) AS FinalEnd
        FROM tblKids INNER JOIN tblSchedules ON (tblKids.KidID = tblSchedules.Ki dID) AND (tblKids.KidID = tblSchedules.Ki dID) AND (tblKids.KidID = tblSchedules.Ki dID)
        GROUP BY tblKids.KidID, IIf([FakeEnd]=#12/31/9999#,Null,[FakeEnd]);
        [/CODE]
        Thanks,
        Bradley
        Last edited by Scott Price; Oct 8 '07, 12:28 AM. Reason: [CODE] Tags

        Comment

        • nico5038
          Recognized Expert Specialist
          • Nov 2006
          • 3080

          #5
          I posted:

          Max(Nz(EndDate, #01-01-2999#))

          and NOT

          NZ(Max(EndDate, #01-01-2999#))

          The problem described about the Max can't happen with the proposed Max(NZ()) statement!

          Nic;o)

          Comment

          • HoganGroup
            New Member
            • Oct 2007
            • 8

            #6
            Thanks to you both for your help. It was Max(Nz(... that I tried but with the same result. Subquerying did work, though. Hooray! In the end, this is the SQL that worked.

            [CODE=sql]SELECT tblKids.KidID, IIf([FakeEnd]=#12/31/9999#,Null,[FakeEnd]) AS FinalEnd
            FROM [SELECT tblKids.KidID, Max(IIf(IsNull([tblSchedules].[EndDate]),#12/31/9999#,[tblSchedules].[EndDate])) AS FakeEnd
            FROM tblKids INNER JOIN tblSchedules ON (tblKids.KidID = tblSchedules.Ki dID) AND (tblKids.KidID = tblSchedules.Ki dID) AND (tblKids.KidID = tblSchedules.Ki dID)
            GROUP BY tblKids.KidID]. AS [%$##@_Alias] INNER JOIN tblKids ON [%$##@_Alias].KidID = tblKids.KidID
            GROUP BY tblKids.KidID, IIf([FakeEnd]=#12/31/9999#,Null,[FakeEnd]);[/CODE]

            Thank you again,
            Bradley

            Comment

            Working...