




CREATE View  [dbo].[View_Project_CQMSData_CQMS]
as
    WITH LatestData AS (
    SELECT
        person.CollCropCode,
        MAX(ReportDate) AS LatestReportDate
    FROM
        dbo.Project_CQMSData_CQMS person
    GROUP BY
        person.CollCropCode
),RankedData AS (
	 SELECT
        cqms.*
    FROM
        dbo.Project_CQMSData_CQMS cqms
    INNER JOIN
        LatestData l ON cqms.CollCropCode = l.CollCropCode AND cqms.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



