我有2个df,它们具有不同的行号和相似的列(n1,datetime,v1)。日期时间不完全匹配;有时少于2分钟,而其他时间则更多。我想创建2个新的dfs(df3,df4),其中我在彼此之间2分钟之内的行(分别是df3和df4以及截短的df1和2)
df1:
SELECT
[UserId]
MAX(CASE WHEN [Date] = '2019-01-01' THEN ISNULL([Param1], NULL) ELSE NULL END) AS [Param1:2019-01-01],
MAX(CASE WHEN [Date] = '2019-01-01' THEN ISNULL([Param2], NULL) ELSE NULL END) AS [Param2:2019-01-01],
MAX(CASE WHEN [Date] = '2019-01-01' THEN ISNULL([Param3], NULL) ELSE NULL END) AS [Param3:2019-01-01],
MAX(CASE WHEN [Date] = '2019-01-02' THEN ISNULL([Param1], NULL) ELSE NULL END) AS [Param1:2019-01-02],
MAX(CASE WHEN [Date] = '2019-01-02' THEN ISNULL([Param2], NULL) ELSE NULL END) AS [Param2:2019-01-02],
MAX(CASE WHEN [Date] = '2019-01-02' THEN ISNULL([Param3], NULL) ELSE NULL END) AS [Param3:2019-01-02],
FROM [dbo].[MyTable]
GROUP BY [UserId]
ORDER BY [UserId]
df2:
n1,Datetime,v1
steel1,2018-07-01 00:01:48.000,34.17
steel1,2018-07-01 00:19:42.000,36.79
steel1,2018-07-01 00:43:15.000,36.42
steel1,2018-07-01 00:49:15.000,36.31
steel1,2018-07-01 00:55:30.000,36.24
steel1,2018-07-01 01:01:15.000,36.09
steel1,2018-07-01 01:07:15.000,35.95
steel1,2018-07-01 01:19:15.000,35.73
steel1,2018-07-01 01:46:16.000,35.21
steel1,2018-07-01 01:49:16.000,35.18
steel1,2018-07-01 02:13:16.000,34.7
steel1,2018-07-01 02:19:22.000,34.56
steel1,2018-07-01 02:25:26.000,34.56
steel1,2018-07-01 02:31:16.000,34.41
steel1,2018-07-01 02:40:16.000,34.3
steel1,2018-07-01 02:43:15.000,34.3
steel1,2018-07-01 02:49:16.000,34.01
steel1,2018-07-01 02:55:15.000,34.01
steel1,2018-07-01 03:01:16.000,33.82
steel1,2018-07-01 03:07:22.000,33.71
steel1,2018-07-01 03:13:16.000,33.71
df3:
n1,Datetime,v1
steel2,2018-07-01 00:00:48.000,32.17
steel2,2018-07-01 00:06:49.000,32.17
steel2,2018-07-01 00:12:48.000,32.17
steel2,2018-07-01 00:18:48.000,32.17
steel2,2018-07-01 00:24:49.000,32.17
steel2,2018-07-01 00:30:48.000,32.17
steel2,2018-07-01 00:36:49.000,32.17
steel2,2018-07-01 00:42:48.000,32.14
steel2,2018-07-01 00:48:49.000,32.14
steel2,2018-07-01 00:54:48.000,32.14
steel2,2018-07-01 01:00:48.000,32.14
steel2,2018-07-01 01:06:49.000,32.1
steel2,2018-07-01 01:12:49.000,32.06
steel2,2018-07-01 01:18:49.000,32.06
steel2,2018-07-01 01:24:49.000,32.03
steel2,2018-07-01 01:30:49.000,32.03
steel2,2018-07-01 01:36:51.000,31.99
steel2,2018-07-01 01:42:49.000,31.95
steel2,2018-07-01 01:48:49.000,31.92
steel2,2018-07-01 01:54:49.000,31.88
steel2,2018-07-01 02:00:50.000,31.84
steel2,2018-07-01 02:06:51.000,31.84
steel2,2018-07-01 02:12:49.000,31.81
steel2,2018-07-01 02:18:51.000,31.77
steel2,2018-07-01 02:24:50.000,31.73
steel2,2018-07-01 02:30:50.000,31.7
steel2,2018-07-01 02:36:50.000,31.66
steel2,2018-07-01 02:42:50.000,31.63
steel2,2018-07-01 02:48:50.000,31.59
steel2,2018-07-01 02:54:50.000,31.52
steel2,2018-07-01 03:00:50.000,31.48
steel2,2018-07-01 03:06:50.000,31.48
steel2,2018-07-01 03:12:50.000,31.41
steel2,2018-07-01 03:18:50.000,31.37
steel2,2018-07-01 03:24:50.000,31.33
steel2,2018-07-01 03:30:50.000,31.3
df4:
n1,Datetime,v1
steel1,2018-07-01 00:01:48.000,34.17
steel1,2018-07-01 00:19:42.000,36.79
steel1,2018-07-01 00:43:15.000,36.42
steel1,2018-07-01 00:49:15.000,36.31
steel1,2018-07-01 00:55:30.000,36.24
steel1,2018-07-01 01:01:15.000,36.09
steel1,2018-07-01 01:07:15.000,35.95
steel1,2018-07-01 01:19:15.000,35.73
steel1,2018-07-01 01:49:16.000,35.18
steel1,2018-07-01 02:13:16.000,34.7
steel1,2018-07-01 02:19:22.000,34.56
steel1,2018-07-01 02:25:26.000,34.56
steel1,2018-07-01 02:31:16.000,34.41
steel1,2018-07-01 02:43:15.000,34.3
steel1,2018-07-01 02:49:16.000,34.01
steel1,2018-07-01 02:55:15.000,34.01
steel1,2018-07-01 03:01:16.000,33.82
steel1,2018-07-01 03:07:22.000,33.71
steel1,2018-07-01 03:13:16.000,33.71
这段代码没有给我我想要的东西:(Datetime是datetime64 [ns])
n1,Datetime,v1
steel2,2018-07-01 00:00:48.000,32.17
steel2,2018-07-01 00:18:48.000,32.17
steel2,2018-07-01 00:42:48.000,32.14
steel2,2018-07-01 00:48:49.000,32.14
steel2,2018-07-01 00:54:48.000,32.14
steel2,2018-07-01 01:00:48.000,32.14
steel2,2018-07-01 01:06:49.000,32.1
steel2,2018-07-01 01:18:49.000,32.06
steel2,2018-07-01 01:48:49.000,31.92
steel2,2018-07-01 02:12:49.000,31.81
steel2,2018-07-01 02:18:51.000,31.77
steel2,2018-07-01 02:24:50.000,31.73
steel2,2018-07-01 02:30:50.000,31.7
steel2,2018-07-01 02:42:50.000,31.63
steel2,2018-07-01 02:48:50.000,31.59
steel2,2018-07-01 02:54:50.000,31.52
steel2,2018-07-01 03:00:50.000,31.48
steel2,2018-07-01 03:06:50.000,31.48
steel2,2018-07-01 03:12:50.000,31.41
感谢您的帮助