我有一个场景,我需要在15秒内平均一次使用列,并将其总计为一小时。对于特定的MID,SID和PID,我现在通过将时间循环增加15秒dateadd(秒,15,@ StartTime)来实现它,但这非常慢。请提出更好的解决方案。请参阅下表。
CREATE TABLE [dbo].[SampleData](
[MID] [varchar](50) NOT NULL,
[StartTime] [datetime] NOT NULL,
[SID] [int] NOT NULL,
[PID] [int] NOT NULL,
[Usage] [decimal](10, 4) NOT NULL
) ON [PRIMARY]
enter code here
示例数据
INSERT [dbo].[SampleData] ([MID], [StartTime], [SID], [PID], [Usage]) VALUES (N'77', CAST(0x0000A64500001068 AS DateTime), 1, 1, CAST(27.0000 AS Decimal(10, 4)))
INSERT [dbo].[SampleData] ([MID], [StartTime], [SID], [PID], [Usage]) VALUES (N'77', CAST(0x0000A645000020D0 AS DateTime), 1, 1, CAST(40.0000 AS Decimal(10, 4)))
INSERT [dbo].[SampleData] ([MID], [StartTime], [SID], [PID], [Usage]) VALUES (N'77', CAST(0x0000A64500003138 AS DateTime), 1, 1, CAST(98.0000 AS Decimal(10, 4)))
INSERT [dbo].[SampleData] ([MID], [StartTime], [SID], [PID], [Usage]) VALUES (N'77', CAST(0x0000A645000041A0 AS DateTime), 1, 1, CAST(58.0000 AS Decimal(10, 4)))
INSERT [dbo].[SampleData] ([MID], [StartTime], [SID], [PID], [Usage]) VALUES (N'77', CAST(0x0000A6450010CCC8 AS DateTime), 1, 1, CAST(74.0000 AS Decimal(10, 4)))
INSERT [dbo].[SampleData] ([MID], [StartTime], [SID], [PID], [Usage]) VALUES (N'77', CAST(0x0000A6450010DD30 AS DateTime), 1, 1, CAST(42.0000 AS Decimal(10, 4)))
INSERT [dbo].[SampleData] ([MID], [StartTime], [SID], [PID], [Usage]) VALUES (N'77', CAST(0x0000A6450010ED98 AS DateTime), 1, 1, CAST(34.0000 AS Decimal(10, 4)))
INSERT [dbo].[SampleData] ([MID], [StartTime], [SID], [PID], [Usage]) VALUES (N'77', CAST(0x0000A6450010FE00 AS DateTime), 1, 1, CAST(36.0000 AS Decimal(10, 4)))
INSERT [dbo].[SampleData] ([MID], [StartTime], [SID], [PID], [Usage]) VALUES (N'77', CAST(0x0000A64500218928 AS DateTime), 1, 1, CAST(84.0000 AS Decimal(10, 4)))
INSERT [dbo].[SampleData] ([MID], [StartTime], [SID], [PID], [Usage]) VALUES (N'77', CAST(0x0000A64500219990 AS DateTime), 1, 1, CAST(19.0000 AS Decimal(10, 4)))
INSERT [dbo].[SampleData] ([MID], [StartTime], [SID], [PID], [Usage]) VALUES (N'77', CAST(0x0000A6450021A9F8 AS DateTime), 1, 1, CAST(40.0000 AS Decimal(10, 4)))
INSERT [dbo].[SampleData] ([MID], [StartTime], [SID], [PID], [Usage]) VALUES (N'77', CAST(0x0000A6450021BA60 AS DateTime), 1, 1, CAST(48.0000 AS Decimal(10, 4)))
INSERT [dbo].[SampleData] ([MID], [StartTime], [SID], [PID], [Usage]) VALUES (N'77', CAST(0x0000A64500324588 AS DateTime), 1, 1, CAST(97.0000 AS Decimal(10, 4)))
INSERT [dbo].[SampleData] ([MID], [StartTime], [SID], [PID], [Usage]) VALUES (N'77', CAST(0x0000A645003255F0 AS DateTime), 1, 1, CAST(48.0000 AS Decimal(10, 4)))
INSERT [dbo].[SampleData] ([MID], [StartTime], [SID], [PID], [Usage]) VALUES (N'77', CAST(0x0000A64500326784 AS DateTime), 1, 1, CAST(60.0000 AS Decimal(10, 4)))
INSERT [dbo].[SampleData] ([MID], [StartTime], [SID], [PID], [Usage]) VALUES (N'77', CAST(0x0000A64500327DC8 AS DateTime), 1, 1, CAST(48.0000 AS Decimal(10, 4)))
答案 0 :(得分:0)
Usage
的平均值。 内部SQL:每分钟(= 15秒)的Usage
的平均值。
SELECT [MID], [SID], [PID],
Yr,
Mn,
Dy,
Hr
SUM(AvgUse) AS HourSumOfUse
FROM
(
SELECT [MID], [SID], [PID],
YEAR([StartTime]) AS Yr,
MONTH([StartTime]) AS Mn,
DAY([StartTime]) AS Dy,
DATEPART ("HH",[StartTime]) AS Hr,
DATEPART ("Mi", [StartTime]) AS Mn,
CAST(DATEPART ("ss", [StartTime])/15 + 1, Int) AS Q_Min,
Avg([Usage]) AS AvgUse
FROM [dbo].[SampleData]
GROUP BY [MID], [SID], [PID],
YEAR([StartTime]),
MONTH([StartTime]),
DAY([StartTime]),
DATEPART ("HH",[StartTime]),
DATEPART ("Mi", [StartTime]),
CAST(DATEPART ("ss", [StartTime])/15 + 1, Int)
)
GROUP BY [MID], [SID], [PID],
Yr,
Mn,
Dy,
Hr