如何在 SQL 中将 5 分钟时间戳间隔转换为 1 分钟间隔以保留其他信息?
How to convert 5min timestamp intervals to 1min intervals in SQL keeping the other information?
看到一两个题目问的是相反的(把1min时间戳变成5min或者其他时间戳间隔)。但是我完全不明白编程这个的想法。
这是我的输入table:
timestamp Food Beverage
15:00:00 Pizza Coke
15:05:00 Burger Coke
15:10:00 Hotdog Pepsi
… … …
这就是我想要的输出:
timestamp Food Beverage
15:00:00 Pizza Coke
15:01:00 Pizza Coke
15:02:00 Pizza Coke
15:03:00 Pizza Coke
15:04:00 Pizza Coke
15:05:00 Burger Coke
15:06:00 Burger Coke
15:07:00 Burger Coke
15:08:00 Burger Coke
15:09:00 Burger Coke
15:10:00 Hotdog Pepsi
15:11:00 Hotdog Pepsi
… … …
提前致谢!
最简单的方法是使用cross join
:
select dateadd(minute, v.m, timestamp) as timestamp,
food, beverage
from t cross join
(values (0), (1), (2), (3), (4)) v(m);
您也可以使用明确的 select
/union all
:
select dateadd(minute, v.m, timestamp) as timestamp,
food, beverage
from t cross join
(select 0 as m union all select 1 union all select 2 union all select 3 union all select 4
) v(m);
您可以使用 DATEADD。参数输入N表示分钟。
ROW_NUMBER 将增加每行的时间戳。
DECLARE @interval varchar(10) = 5
DROP TABLE IF EXISTS #data
SELECT timestamp='15:00:00',Food='Pizza',Beverage='Coke'
INTO #data
UNION ALL SELECT timestamp='15:05:00',Food='Burger',Beverage='Coke'
UNION ALL SELECT timestamp='15:10:00',Food='Pizza',Beverage='Pepsi'
DROP TABLE IF EXISTS #TAB
CREATE TABLE #TAB (id int)
EXEC ('INSERT INTO #TAB
SELECT TOP '+@interval+ ' ROW_NUMBER() OVER(ORDER BY name) FROM sys.columns')
SELECT timestamp=CAST(DATEADD(n,ROW_NUMBER() OVER (PARTITION BY timestamp,food,beverage ORDER BY Food),timestamp) as time) , Food,Beverage
FROM #data
CROSS JOIN #TAB
ORDER BY 1
输出将是:
+------------------+--------+----------+
| timestamp | Food | Beverage |
+------------------+--------+----------+
| 15:01:00.0000000 | Pizza | Coke |
| 15:02:00.0000000 | Pizza | Coke |
| 15:03:00.0000000 | Pizza | Coke |
| 15:04:00.0000000 | Pizza | Coke |
| 15:05:00.0000000 | Pizza | Coke |
| 15:06:00.0000000 | Burger | Coke |
| 15:07:00.0000000 | Burger | Coke |
| 15:08:00.0000000 | Burger | Coke |
| 15:09:00.0000000 | Burger | Coke |
| 15:10:00.0000000 | Burger | Coke |
| 15:11:00.0000000 | Pizza | Pepsi |
| 15:12:00.0000000 | Pizza | Pepsi |
| 15:13:00.0000000 | Pizza | Pepsi |
| 15:14:00.0000000 | Pizza | Pepsi |
| 15:15:00.0000000 | Pizza | Pepsi |
+------------------+--------+----------+
看到一两个题目问的是相反的(把1min时间戳变成5min或者其他时间戳间隔)。但是我完全不明白编程这个的想法。
这是我的输入table:
timestamp Food Beverage
15:00:00 Pizza Coke
15:05:00 Burger Coke
15:10:00 Hotdog Pepsi
… … …
这就是我想要的输出:
timestamp Food Beverage
15:00:00 Pizza Coke
15:01:00 Pizza Coke
15:02:00 Pizza Coke
15:03:00 Pizza Coke
15:04:00 Pizza Coke
15:05:00 Burger Coke
15:06:00 Burger Coke
15:07:00 Burger Coke
15:08:00 Burger Coke
15:09:00 Burger Coke
15:10:00 Hotdog Pepsi
15:11:00 Hotdog Pepsi
… … …
提前致谢!
最简单的方法是使用cross join
:
select dateadd(minute, v.m, timestamp) as timestamp,
food, beverage
from t cross join
(values (0), (1), (2), (3), (4)) v(m);
您也可以使用明确的 select
/union all
:
select dateadd(minute, v.m, timestamp) as timestamp,
food, beverage
from t cross join
(select 0 as m union all select 1 union all select 2 union all select 3 union all select 4
) v(m);
您可以使用 DATEADD。参数输入N表示分钟。
ROW_NUMBER 将增加每行的时间戳。
DECLARE @interval varchar(10) = 5
DROP TABLE IF EXISTS #data
SELECT timestamp='15:00:00',Food='Pizza',Beverage='Coke'
INTO #data
UNION ALL SELECT timestamp='15:05:00',Food='Burger',Beverage='Coke'
UNION ALL SELECT timestamp='15:10:00',Food='Pizza',Beverage='Pepsi'
DROP TABLE IF EXISTS #TAB
CREATE TABLE #TAB (id int)
EXEC ('INSERT INTO #TAB
SELECT TOP '+@interval+ ' ROW_NUMBER() OVER(ORDER BY name) FROM sys.columns')
SELECT timestamp=CAST(DATEADD(n,ROW_NUMBER() OVER (PARTITION BY timestamp,food,beverage ORDER BY Food),timestamp) as time) , Food,Beverage
FROM #data
CROSS JOIN #TAB
ORDER BY 1
输出将是:
+------------------+--------+----------+
| timestamp | Food | Beverage |
+------------------+--------+----------+
| 15:01:00.0000000 | Pizza | Coke |
| 15:02:00.0000000 | Pizza | Coke |
| 15:03:00.0000000 | Pizza | Coke |
| 15:04:00.0000000 | Pizza | Coke |
| 15:05:00.0000000 | Pizza | Coke |
| 15:06:00.0000000 | Burger | Coke |
| 15:07:00.0000000 | Burger | Coke |
| 15:08:00.0000000 | Burger | Coke |
| 15:09:00.0000000 | Burger | Coke |
| 15:10:00.0000000 | Burger | Coke |
| 15:11:00.0000000 | Pizza | Pepsi |
| 15:12:00.0000000 | Pizza | Pepsi |
| 15:13:00.0000000 | Pizza | Pepsi |
| 15:14:00.0000000 | Pizza | Pepsi |
| 15:15:00.0000000 | Pizza | Pepsi |
+------------------+--------+----------+