根据日期

时间:2018-01-19 12:17:16

标签: tsql join

我需要加入两组数据。 但是,没有ID或类似的加入集合,两个集合之间唯一的共同因素是使用的日期。 不幸的是,日期不是100%相同,通常是两个日期之间的差异几分钟或几秒。 我希望如果例如日期之间的差异小于10分钟,则可以根据日期加入。

这是可能的吗?

示例数据:

> EventDate                 WindSpeed 
> 2018-01-09 12:00:18.000   3 
> 2018-01-10 12:00:03.000   4

然后加入:

> ReadingDate               ReadingValue
> 2018-01-09 12:00:00.000   4,6
> 2018-01-10 12:00:00.000   5

到目前为止,我有两个以下的查询,我无法自己加入,感谢任何帮助或提示让这个工作:

SELECT tu.Name,
   tv.VoyageNo,
   tv.ExternalVoyageNo,
   tve.VoyageEventCode,
   tve.EventDate,
   tve.WindSpeed,
   tve.WindDirection,
   tve.SeaStateCode,
   tve.PositionLatitude,
   tve.PositionLongitude,
   tve.Speed,
   tve.DistanceByLog,
   tve.DistanceOverGround,
   tve.HFOStock,
   tve.LSFOStock,
   tve.MDOStock,
   tve.MGOStock,
   tve.DraughxcID,
   CASE
       WHEN ts.IsBallast = 0
       THEN 'Laden'
       WHEN ts.IsBallast = 1
       THEN 'Ballast'
       ELSE 'In Port'
   END AS Condition,
   tcf.Mt
FROM dbo.xcVoyageEvent tve
 INNER JOIN dbo.xcUnit tu ON tve.xcUnitID = tu.xcUnitID
 INNER JOIN dbo.xcVoyage tv ON tve.xcVoyageID = tv.xcVoyageID
 LEFT JOIN dbo.xcSailing ts ON tve.xcSailingID = ts.xcSailingID
 LEFT JOIN dbo.xcCargo tc ON tve.xcVoyageID = tc.xcVoyageID
 LEFT JOIN dbo.xcCargoFigure tcf ON tc.xcCargoID = tcf.xcCargoID
WHERE tve.RowDeleted = 0
  AND tv.RowDeleted = 0
  AND ts.RowDeleted = 0
  AND tu.RowDeleted = 0
  AND tve.VoyageEventCode IN('Commence Sea Passage', 'Sea Passage 
Suspended', 'Sea Passage Resumed', 'End Of Sea Passage', 'xcdailyreport', 
'Noon position', 'morning position', 'voyage commenced', 'voyage complete', 
'Enter Magallanes Strait', 'Enter Suez Canal', 'All Clear', 'All Fast')
 ORDER BY tu.Name,
     tve.EventDate;   

SELECT tu.name,
   tc.Code,
   tc.Name,
   xc.Name,
   xcr.ReadingValue,
   xcr.ReadingDate,
   xcr.Comment
FROM dbo.xcMeasurement xc
 INNER JOIN dbo.xcMeasurementReading xcr ON xc.xcMeasurementID = 
xcr.xcMeasurementID
 INNER JOIN dbo.xcComponent tc ON xc.xcComponentID = tc.xcComponentID
 INNER JOIN dbo.xcUnit tu ON xc.xcUnitID = tu.xcUnitID
WHERE xc.RowDeleted = 0
  AND xcr.RowDeleted = 0
  AND tu.RowDeleted = 0
  AND xc.Consumption = 1
ORDER BY tu.Name,
     tc.code,
     xcr.ReadingDate

2 个答案:

答案 0 :(得分:2)

要在10分钟内获取带有EventDate和ReadingDate的行,请将其添加到where语句:

ABS(DATEDIFF(MINUTE, EventDate, ReadingDate)) <= 10

答案 1 :(得分:2)

您提到不同表格中的两个日期之间存在差异

如果每天只有1个“事件”,您可以加入日期或日期时间。

如果你每天有多个事件,你还必须考虑在我的例子中使用Datediff我使用600秒差异

请参阅下面的示例

create table #temp1
( EventDate Datetime2, Windspeed int)

create table #temp2
( Readingdate Datetime2, Readingvalue nvarchar(50))

insert into #temp1 Values
('2018-01-09 12:00:18.000', 3),
('2018-01-09 13:10:00.000', 3),
('2018-01-10 12:00:03.000', 4)

insert into #temp2 Values
('2018-01-09 12:00:00.000', '5,6'),
('2018-01-09 13:00:00.000', '6'),
('2018-01-10 12:00:00.000', '5')


SELECT
    *,DATEDIFF(S,T2.Readingdate, T1.EventDate) [dif_in_sec]
FROM #temp1 AS T1
INNER JOIN #TEMP2 AS T2 ON  CAST(T1.EventDate AS date) = CAST(T2.Readingdate AS DATE) 
                            AND(     DATEDIFF(S,T2.Readingdate, T1.EventDate) <= 600 
                                 and DATEDIFF(S,T2.Readingdate, T1.EventDate) >= 0)

您可能需要调整值