


CREATE View  [dbo].[View_Project_HJGLData_HJGL]
as
WITH LatestData AS (
    SELECT
        person.CollCropCode,
        MAX(ReportDate) AS LatestReportDate
    FROM
        dbo.Project_HJGLData_HJGL person
    GROUP BY
        person.CollCropCode
),RankedData AS (
	 SELECT
        hjgl.*
    FROM
        dbo.Project_HJGLData_HJGL hjgl
    INNER JOIN
        LatestData l ON hjgl.CollCropCode = l.CollCropCode AND hjgl.ReportDate = l.LatestReportDate
)
SELECT
    pro.ProjectName,
    pro.ShortName,
    rd.*
FROM RankedData rd
LEFT JOIN Base_Project pro ON pro.ProjectId = rd.ProjectId
where pro.ProjectName !='' and pro.ProjectState='1'


GO


