Domanda

I'm currently writing a query for Visual Studio 2012 and testing it in Microsoft SQL Server Management Studio using SQL Server 2008 R2.

At the moment, I've read through MSDN's article on datetimes and DATEADD, but it seems like my syntax is right. I've also read some stuff on Google as well as How to select last one week data from today's date and MySQL: DATE_ADD as well as a few more Stack Overflow articles.

The query I'm running at the moment is really simple, just:

SELECT [DateTime] AS 'Time'
      ,[RawStatus] AS 'Data'
  FROM [ADatabase].[dbo].[SomeTable]
  WHERE CustomPollerAssignmentID = '6570267A-22E1-4556-B344-EB27D9831419' --Latency Poller
    AND RowID = 000042 --Some Modem Number
    AND DATEADD(HOUR, -1, CURRENT_TIMESTAMP) <= DateTime 
  ORDER BY DateTime DESC

What I was expecting this to do was to return the data (network latency in this case) for the last hour. Instead, it's returning the last three hours and thirty minutes. When running the code with the DATEADD statement commented out, it runs just fine and returns everything for the past day or two, the maximum time this table stores latency data.

Now, the strange code above is modeled after what's below, which I know works:

SELECT  NMSDS.[SnapshotTimestamp] AS 'Time'
      ,[LatencyValue] AS 'Latency'
  FROM [ADifferentDatabase].[dbo].[AnotherTable] Late
  INNER JOIN ADifferentDatabase.dbo.YetAnotherTable NMSDS ON NMS_Id = 1 
    AND NMSDS.SnapshotID = Late.SnapshotID
  WHERE DATEADD(HOUR, -6, CURRENT_TIMESTAMP) <= NMSDS.SnapshotTimestamp 
    AND InrouteGroupId = @IRID 
    AND NetworkId = @NTID
  ORDER BY [Late].SnapshotID ASC

My questions are:

  1. What am I missing?
  2. Have I formatted my query wrong? And the second is why would it return 3.5 hours instead of one given the fact that the second query actually works and returns things properly?
È stato utile?

Soluzione

I would have to say that you are missing timezone data. From your queries, there is no data what timezone the servers are in, or what timezone they are inserting data to.

Altri suggerimenti

What exactly do you want?? Do you want the data for the last clock hour? (i.e., if it's 10:37 you want all data between 9:00 and 10:00)

or do you want the data for the past one hour? (i.e., if it's 10:37:12 you want all data between 9:37:13 and 10:37:12)

For the first, change the where clause to

...And NMSDS.SnapshotTimestamp >= 
            DateAdd(hour, datediff(hour, 0, Current_Timestamp)-1, 0), 
   And NMSDS.SnapshotTimestamp <  
            DateAdd(hour, datediff(hour, 0, Current_Timestamp), 0)

For the second, it's easier...

 ... And NMSDS.SnapshotTimestamp > Current_Timestamp - 1/24

but I admit I'm really confused by the value 6 in the second query... Why a 6, if you are trying for the data for the last single hour ??

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top