Greetings, everyone.
I’m working primarily with the HDCase table that contains two specific fields related to date/time:
Erp.HDCase.CreatedDate {date}
Erp.HDCase.CreatedTime {int}
I’m sure Epicor has a perfectly sound strategic reason to use (Julian?) values for the time field, but that’s throwing another level of complexity into the mixing bowl. I can use something like:
MAX(CONVERT(varchar, DATEADD(ms, (HDCase.CreatedTime * 1000), 0), 108))
…perhaps, to pull that back in as a legitimate time, but for what I’m trying to do, date and time really needs to be evaluated as, well,… date and time.
What I’m hoping to accomplish is to isolate the most recent case for each distinct ProjectID (also included in the HDCase table -----> HDCase.ProjectID). So, for as many distinct Projects that exist either in the HDCase table, or in the Project table itself, I need to return the latest case entered/opened by date.
Consider that there could be more than one case opened on any given date. That’s where either the [HDCase.CreatedTime], or the [HDCase.HDCaseNum] needs to come into play to return the lasted case occurring by time for the latest case day, if there exists more than one case for a specific date.
Here’s how I think I need to tackle this: Create “SubQuery1” to isolate the latest case date for each Project by MAX(HDCase.CreatedDate), grouping by HDCase.ProjectID at the very least. After that, I would need to create a “SubQuery2” to isolate the MAX(HDCase.CreatedTime) for each “maxed created date”, grouping by HDCase.ProjectID, then by MAX(HDCase.CreatedDate).
From what I understand, aggregates cannot be used in GROUP BY evaluations - and that’s where I am drawing a bit of a blank concerning what I really should be doing instead.
Would anyone wish to realign my thinking and point me in the right direction. Seems like a pretty common thing to do, but I’m not thinking it through very well.