I have a grouped table that looks like this:
|End_of_Month_Date|VersionId|
|-----------------|---------|
|2024-06-30 |1 |
|2024-06-30 |2 |
|2024-07-31 |1 |
|2024-07-31 |2 |
|2024-07-31 |3 |
|2024-07-31 |5 |
For each month end, I want to pull in the
Max(VersionId)
so that in the end I have a unique End_of_Month_Date
and max(VersionId)
.
|End_of_Month_Date|VersionId|
|-----------------|---------|
|2024-06-30 |2 |
|2024-07-31 |5 |
I've tried writing the code, but it's not working correctly:
select distinct c.END_OF_MONTH_DATE, c.VERSION_ID, 'Assets'
from dbo.Contracts_Version_Details c
inner join(
select distinct max(c1.Version_Id) as MaxVersionId, c1.END_OF_MONTH_DATE
from dbo.Contracts_Version_Details c1
group by c1.End_of_month_date) mv
on c.VERSION_ID = mv.MaxVersionId
inner join(
select distinct max(c2.END_OF_MONTH_DATE) as MaxDate, c2.VERSION_ID
from dbo.Contracts_Version_Details c2
group by c2.VERSION_ID
) as md
on c.END_OF_MONTH_DATE = md.MaxDate
Asked by Daylon Hunt
(27 rep)
Dec 18, 2024, 10:04 PM
Last activity: Jan 3, 2025, 10:51 AM
Last activity: Jan 3, 2025, 10:51 AM