2017-12-12 12 views
0

Ich wollte den Unterschied in HH anzuzeigen: MM: SS zwischen zwei datetime Feldern in SQL Server 2014Wie funktioniert diese Zeitdifferenzberechnung?

ich in diesen Stack Overflow post eine Lösung gefunden. Und es funktioniert perfekt. Aber ich möchte das "Warum" verstehen, wie das zur richtigen Antwort kommt.

T-SQL:

SELECT y.CustomerID , 
    y.createDate , 
    y.HarvestDate , 
    y.DateDif , 
    DATEDIFF (DAY, 0, y.DateDif) AS [Days] , 
    DATEPART (HOUR, y.DateDif) AS [Hours] , 
    DATEPART (MINUTE, y.DateDif) AS [Minutes] 
FROM (
     SELECT x.createDate - x.HarvestDate AS [DateDif] , 
       x.createDate , 
       x.HarvestDate , 
       x.CustomerID 
     FROM (
        SELECT CustomerID , 
         HarvestDate , 
         createDate 
        FROM dbo.CustomerHarvestReports 
        WHERE HarvestDate >= DATEADD (MONTH, -6, GETDATE()) 
      ) AS [x] 
    ) AS [y] 
ORDER BY DATEDIFF (DAY, 0, y.DateDif) DESC; 

Ergebnisse:

1239090 2017-11-07 08:51:03.870 2017-10-14 11:39:49.540 1900-01-24 21:11:14.330 23 21 11 
1239090 2017-11-07 08:51:04.823 2017-10-19 11:17:48.320 1900-01-19 21:33:16.503 18 21 33 
1843212 2017-10-27 19:14:02.070 2017-10-21 10:49:57.733 1900-01-07 08:24:04.337 6 8 24 
1843212 2017-10-27 19:14:03.057 2017-10-21 10:49:57.733 1900-01-07 08:24:05.323 6 8 24 

Die erste Spalte in Kunden-ID - die zweite und dritte Spalte sind die Spalten I zwischen der Zeitdifferenz berechnen wollte. Die dritte Spalte ist der Unterschied zwischen den zwei Spalten - und einer der Punkte im Code, in dem ich nicht verstehe.

Wenn Sie zwei datetime Felder wie dieses create date - harvestdate subtrahieren, warum wird das Jahr 1900 standardmäßig verwendet?

Und in Bezug auf DATEDIFF (DAY, 0 , y.DateDiff) - was bedeutet die 0? Setzt die 0 das Datum auf '01 -01-1900 '?

Es funktioniert - dafür bin ich dankbar. Ich hatte gehofft, ich könnte eine Erklärung bekommen, warum dieses Verhalten funktioniert?

Antwort

1

Ich habe einige Kommentare hinzugefügt, die es erklären sollte:

SELECT y.CustomerID , 
    y.createDate , 
    y.HarvestDate , 
    y.DateDif , 
    DATEDIFF (DAY, 0, y.DateDif) AS [Days] , -- calculates the number of whole days between 0 and the difference 

    DATEPART (HOUR, y.DateDif) AS [Hours] , -- the number of hours between the two dates has already been cleverly 
              -- calculated in [DateDif], therefore, all that is required is to extract 
              -- that figure using DATEPART 

    DATEPART (MINUTE, y.DateDif) AS [Minutes] -- same explanation as [Hours] 
FROM (
     SELECT x.createDate - x.HarvestDate AS [DateDif] , -- calculates the difference expressed as a datetime; 
                  -- 0 is '1900-01-01 00:00:00.000' as a datetime, so the 
                  -- resulting datetime will be that plus the difference 
       x.createDate , 
       x.HarvestDate , 
       x.CustomerID 
     FROM (
        SELECT CustomerID , 
         HarvestDate , 
         createDate 
        FROM dbo.CustomerHarvestReports 
        WHERE HarvestDate >= DATEADD (MONTH, -6, GETDATE()) 
      ) AS [x] 
    ) AS [y] 
ORDER BY DATEDIFF (DAY, 0, y.DateDif) DESC; 
+0

Schätzen Sie die Antwort. Das hilft mir zu verstehen, was hier passiert. – MISNole