我需要獲取當前月份和上個月的最後記錄值。每月大約有4,600條記錄。
檢索當前月份和上個月的最後一條記錄
以下是我已經嘗試了代碼,但它返回「0」爲兩個月,而不是價值:
SELECT a.LogPoint as [Meter]
,max(CASE WHEN c.DateTimeStamp = dateadd(MM,-1,getdate()) THEN c.FloatVALUE ELSE 0 END) as [Total LAST Month]
,max(CASE WHEN c.DateTimeStamp = getdate() THEN c.FloatVALUE ELSE 0 END) as [Total This Month]
FROM
SWR.dbo.LoggedEntities a
,SWR.dbo.TrendLogRelation b
,SWR.dbo.LogTimeValues c
WHERE
a.GUID = b .GUID
AND a.Type LIKE 'trend.ETLog'
AND a.LogPoint = 'WsumOut_Trnd'
AND b.EntityID = c.ParentID
GROUP BY a.LogPoint
任何幫助將不勝感激。
乾杯。
看看EOMONTH()命令:https://msdn.microsoft.com/en-us/library/hh213020.aspx。這可能會節省一些頭痛 – LordBaconPants