简体   繁体   English

检索当前月份和上个月的最后一条记录

[英]Retrieve last record from current month and previous month


I need to get the last recorded value from the current month and the previous month. 我需要获取当前月份和上个月的最后记录的值。 There are roughly 4,600 records per month. 每月大约有4,600条记录。

The following is the code I have tried, however it returns '0' for the two months and not the value: 以下是我尝试过的代码,但是在两个月中它返回“ 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

Any help would be greatly appreciated. 任何帮助将不胜感激。
Cheers. 干杯。

I assume the LogPoint is the primary key. 我假设LogPoint是主键。 correct? 正确? In that case check following: 在这种情况下,请检查以下内容:

    SELECT mainA.LogPoint AS [Meter],
       lastMonth.FloatValue AS [Total LAST Month],
       thisMonth.FloatValue AS [Total This Month]
FROM SWR.dbo.LoggedEntities mainA
     CROSS APPLY
     (
       SELECT TOP 1 c.FloatVALUE
       FROM SWR.dbo.LoggedEntities a
           JOIN SWR.dbo.TrendLogRelation b ON a.GUID = b.GUID
           JOIN SWR.dbo.LogTimeValues c ON b.EntityID = c.ParentID
       WHERE a.LogPoint = mainA.LogPoint 
       ORDER BY c.DateTimeStamp DESC
     ) thisMonth
    CROSS APPLY
     (
       SELECT TOP 1 c.FloatVALUE
       FROM SWR.dbo.LoggedEntities a
           JOIN SWR.dbo.TrendLogRelation b ON a.GUID = b.GUID
           JOIN SWR.dbo.LogTimeValues c ON b.EntityID = c.ParentID
       WHERE a.LogPoint = mainA.LogPoint AND c.DateTimeStamp <= DATEADD(MM,-1,GETDATE()) 
       ORDER BY c.DateTimeStamp DESC
     ) lastMonth
WHERE a.Type LIKE 'trend.ETLog'
      AND a.LogPoint = 'WsumOut_Trnd';

Just realized that I missed the last month date check. 刚意识到我错过了上个月的日期检查。 added now. 现在添加。 try that :) 尝试:)

getdate() includes both the time and the date, which is why you aren't getting any matches. getdate()包括时间和日期,这就是为什么没有任何匹配的原因。

One option is to cast both values to dates and then do the comparison. 一种选择是将两个值都转换为日期,然后进行比较。

Two important points before I start: 在我开始之前,有两个要点:

  1. Never use commas in the FROM clause. 请勿FROM子句中使用逗号。 Always use explicit JOIN syntax. 始终使用显式的JOIN语法。
  2. Table aliases should be abbreviations for the table. 表别名应为表的缩写。

Then, you want to use row_number() : 然后,您要使用row_number()

SELECT LogPoint as [Meter],
       max(CASE WHEN seqnum = 1 AND
                     DATEDIFF(month, DateTimeStamp, getdate()) = 1
                THEN cltv.FloatVALUE
           END) as [Total LAST Month],
       max(CASE WHEN seqnum = 1 AND
                     DATEDIFF(month, DateTimeStamp, getdate()) = 0
                THEN ltv.FloatVALUE 
           END) as [Total This Month]
FROM (SELECT le.LogPoint, ltv.DateTimeStamp,
             ROW_NUMBER() OVER (PARTITION BY YEAR(DateTimeStamp), MONTH(DateTimeStamp)
                                ORDER BY DateTimeStamp DESC
                               ) as seqnum
      FROM SWR.dbo.LoggedEntities le JOIN
           SWR.dbo.TrendLogRelation tlr
           ON le.GUID = tlr.GUID JOIN
           SWR.dbo.LogTimeValues ltv
           ON ltr.EntityID = ltv.ParentID
      WHERE le.Type LIKE 'trend.ETLog' AND
            le.LogPoint = 'WsumOut_Trnd' AND
            DATEDIFF(month, ltv.DateTimeStamp, getdate()) IN (0, 1)
     ) x
WHERE seqnum = 1;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM