我有一个如下所示的分组表:
月末日期 | 版本号 |
---|---|
2024-06-30 | 1 |
2024-06-30 | 2 |
2024-07-31 | 1 |
2024-07-31 | 2 |
2024-07-31 | 3 |
2024-07-31 | 5 |
对于每个月末,我都希望提取最大版本 ID,以便最终获得唯一的月末日期和最大值 (VersionId)。我尝试编写代码,但无法正常工作。
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
这个怎么样?
这可能会让你更接近:
MAX(VersionId) OVER (PARTITION BY End_of_Month_Date)
就足够了。