我有一个15分钟格式的时间序列,其中包含四个不同的变量。我需要将其转换为10分钟的时间序列格式。由于某些限制,我需要在SQL中执行此操作,我认为这可能是使用该语言的较差选择。但是我坚信这是可行的。
我认为需要做的是将每个15min数据点划分为三个5min数据点,然后将每两个5min数据点相加为一个10min数据点。手动执行此操作可以为我提供正确的结果。
下面显示初始表的一些示例DDL SQL:
IF OBJECT_ID('tempdb..#Temp15min') IS NOT NULL DROP TABLE #Temp15min
CREATE TABLE #Temp15min
(
Project_id INT,
Project_name VARCHAR(25),
Date_results DATETIME,
Pplus INT,
Pminus INT,
Qplus INT,
Qminus INT,
Zone VARCHAR(2)
)
INSERT INTO #Temp15min (Project_id, Project_name, Date_results, Pplus, Pminus, Qplus, Qminus, Zone)
VALUES
(1, 'USA_Project', '2020-01-09 00:00',9879, 7766, 3886, 6628, 'US'),
(1, 'USA_Project', '2020-01-09 00:15', 15420, 10969, 5743, 9568, 'US'),
(1, 'USA_Project', '2020-01-09 00:30', 8763, 6602, 5475, 512, 'US'),
(1, 'USA_Project', '2020-01-09 00:45', 6998, 1083, 277, 13360, 'US'),
(1, 'USA_Project', '2020-01-09 01:00', 14356, 275, 1580, 14184, 'US'),
(1, 'USA_Project', '2020-01-09 01:15', 22983, 6161, 7815, 6917, 'US'),
(1, 'USA_Project', '2020-01-09 01:30', 7653, 5080, 2742, 8086, 'US'),
(1, 'USA_Project', '2020-01-09 01:45', 276, 14977, 10318, 3485, 'US')
最终结果表:
IF OBJECT_ID('tempdb..#Temp10min') IS NOT NULL DROP TABLE #Temp10min
CREATE TABLE #Temp10min
(
Project_id INT,
Project_name VARCHAR(25),
Date_results DATETIME,
Pplus INT,
Pminus INT,
Qplus INT,
Qminus INT,
Zone VARCHAR(2)
)
INSERT INTO #Temp10min (Project_id, Project_name, Date_results, Pplus, Pminus, Qplus, Qminus, Zone)
VALUES
(1, 'USA_Project', '2020-01-09 00:00', 6586, 5177, 2591, 4419, 'US'),
(1, 'USA_Project', '2020-01-09 00:10', 8433, 6245, 3210, 5399, 'US'),
(1, 'USA_Project', '2020-01-09 00:20', 10280, 7313, 3829, 6379, 'US'),
(1, 'USA_Project', '2020-01-09 00:30', 5842, 4401, 3650, 341, 'US'),
(1, 'USA_Project', '2020-01-09 00:40', 5254, 2562, 1917, 4624, 'US'),
(1, 'USA_Project', '2020-01-09 00:50', 4665, 722, 185, 8907, 'US'),
(1, 'USA_Project', '2020-01-09 00:00', 9571, 183, 1053, 9456, 'US'),
(1, 'USA_Project', '2020-01-09 00:10', 12446, 2145, 3132, 7034, 'US'),
(1, 'USA_Project', '2020-01-09 00:20', 15322, 4107, 5210, 4611, 'US'),
(1, 'USA_Project', '2020-01-09 00:30', 5102, 3387, 1828, 5391, 'US'),
(1, 'USA_Project', '2020-01-09 00:40', 2643, 6686, 4353, 3857, 'US'),
(1, 'USA_Project', '2020-01-09 00:50', 184, 9985, 6879, 2323, 'US')
进行健全性检查以确保数字从头到尾都是正确的:
SELECT Project_id, Project_name, SUM(Pplus) PplusTotal, SUM(Pminus) PminusTotal, Sum(Qplus) QplusTotal, Sum(Qminus) QminusTotal, Zone FROM #Temp15min GROUP BY Project_id, Project_name, Zone
UNION
SELECT Project_id, Project_name, SUM(Pplus), SUM(Pminus), Sum(Qplus), Sum(Qminus), Zone FROM #Temp10min GROUP BY Project_id, Project_name, Zone
从图形上看,转换是这样的:
我了解请求的复杂性,因此非常感谢您的帮助!
答案 0 :(得分:1)
根据您的描述,您可以在每一分钟内“取消枢纽”,然后重新汇总:
select Project_id, Project_name, min(dateadd(minute, v.minutes, t.dateresults)) as dt,
t.zone,
sum(Pplus / 3.0),
sum(Pminus / 3.0),
sum(Qplus / 3.0),
sum(Qminus / 3.0)
from #Temp15min t cross join
(values (0), (5), (10)) v(minutes)
group by convert(date, dateadd(minute, v.minutes, t.dateresults)),
datepart(hour, dateadd(minute, v.minutes, t.dateresults)),
datepart(minute, dateadd(minute, v.minutes, t.dateresults)) / 6,
Project_id, Project_name, zone;
答案 1 :(得分:0)
基于戈登·利诺夫(Gordon Linoff)的答案以及此other stackoverflow post并适合SQL Server,我使用了以下代码:
IF OBJECT_ID('tempdb..#Tempminutes') IS NOT NULL DROP TABLE #Tempminutes
CREATE TABLE #Tempminutes
(
Minutes INT
)
INSERT INTO #Tempminutes VALUES (0),(5),(10)
select Project_id, Project_name, min(dateadd(minute, v.minutes, t.date_results)) as dt,
t.zone,
sum(Pplus / 3.0),
sum(Pminus / 3.0),
sum(Qplus / 3.0),
sum(Qminus / 3.0)
from #Temp15min t cross join
#Tempminutes v
GROUP BY
DATEPART(YEAR, dateadd(minute, v.minutes, t.date_results)),
DATEPART(MONTH, dateadd(minute, v.minutes, t.date_results)),
DATEPART(DAY, dateadd(minute, v.minutes, t.date_results)),
DATEPART(HOUR, dateadd(minute, v.minutes, t.date_results)),
(DATEPART(MINUTE, dateadd(minute, v.minutes, t.date_results)) / 10),
Project_id, Project_name, zone