关联取时间最后一条数据

WITH aa AS
(SELECT a.keyid FROM Pro_PropertyRealSurveys a
INNER JOIN Com_Photos b ON a.keyid=b.OriginalObjectKeyId
INNER JOIN dbo.Sys_SysParameterItems c ON b.PhotoTypeKeyId=c.keyid
INNER JOIN dbo.Sys_SysParameters d ON c.SysParameterKeyId=d.keyid AND d.ParameterName='室内图'
WHERE a.AuditStatus=1 AND a.IsDelete=0 AND b.IsDelete=0
GROUP BY a.keyid
HAVING(COUNT(1))>=3)

SELECT DISTINCT p.KeyId,p.PropertyStatusCategory,p.EstateKeyId,p.BuildingKeyId,p.HouseKeyId,p.PropertyNo,p.TrustType,p.CountF,p.CountT,p.CountW,p.Floor,
P.HouseDirectionKeyId,p.DecorationSituationKeyId,PropertyUsageKeyId,p.IsDelete,p.Square FROM dbo.Pro_Propertys P
JOIN Est_Houses house WITH(NOLOCK) ON house.KeyId = p.HouseKeyId AND house.HouseStatus = 1
INNER JOIN
Per_A_DeptEstateBuildingPermisstions EstPer ON p.EstateKeyId=EstPer.EstateKeyId AND p.BuildingKeyId=EstPer.BuildingKeyId
INNER JOIN dbo.Pro_PropertyRealSurveys r ON r.PropertyKeyId=p.KeyId

INNER JOIN
(
SELECT PropertyKeyId
 FROM (
SELECT a.keyid,a.propertykeyid,ROW_NUMBER()OVER(PARTITION BY a.propertykeyid ORDER BY a.realsurveydate DESC) num FROM Pro_PropertyRealSurveys a
WHERE a.AuditStatus=1 AND a.IsDelete=0
) a
WHERE num=1 AND a.keyid IN (SELECT keyid FROM aa)
) zz ON p.keyid=zz.PropertyKeyId

WHERE p.IsDelete=0 --AND p.PropertyStatusCategory = 1

posted on 2019-08-13 13:57  梨窝★浅笑  阅读(380)  评论(0编辑  收藏  举报

导航