我有一个复杂的SQL查询请解决。以下是详细信息。
我认为我附上的屏幕截图显示了所有要求。系统就像这样工作。
我正在我的应用程序中创建一个文本文件,并在SQL Server中也记录它。创建此文件后,使用相同名称,另一个文件将以.html扩展名创建。我也在SQL Server中记录html文件。所以基本上如果你想要你可以根据文件名对行进行分组以获得单个请求。
现在通过使用SQL我想获得这两个文件之间的时差。正如您在图像中看到的那样,第一次请求已经获得了547毫秒的收益。
我想以一分钟或一小时的速度获得所有请求的平均时间。
SQL脚本
CREATE TABLE [dbo].[DebugInfo](
[Id] [bigint] IDENTITY(1,1) NOT NULL,
[FileName] [varchar](250) NULL,
[Message] [varchar](250) NULL,
[Time] [datetime] NULL,
[UserId] [int] NULL,
CONSTRAINT [PK_DebugInfo] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
SET IDENTITY_INSERT [dbo].[DebugInfo] ON
INSERT [dbo].[DebugInfo] ([Id], [FileName], [Message], [Time], [UserId]) VALUES (1, N'\\172.16.1.18\datadrive\vol2\SCANNING\SCAN\NovToWin\SIP1\WH000001.txt', N'File Is LOCKED Count:- 511', CAST(0x0000A43300CF83FD AS DateTime), NULL)
INSERT [dbo].[DebugInfo] ([Id], [FileName], [Message], [Time], [UserId]) VALUES (2, N'\\172.16.1.18\datadrive\vol2\SCANNING\SCAN\NovToWin\SIP1\WH000001.htm', N'File Is LOCKED Count:- 335', CAST(0x0000A43300CF84A1 AS DateTime), NULL)
INSERT [dbo].[DebugInfo] ([Id], [FileName], [Message], [Time], [UserId]) VALUES (3, N'\\172.16.1.18\datadrive\vol2\SCANNING\SCAN\NovToWin\SIP1\WH000002.txt', N'File Is LOCKED Count:- 672', CAST(0x0000A43300D0072D AS DateTime), NULL)
INSERT [dbo].[DebugInfo] ([Id], [FileName], [Message], [Time], [UserId]) VALUES (4, N'\\172.16.1.18\datadrive\vol2\SCANNING\SCAN\NovToWin\SIP1\WH000002.htm', N'File Is LOCKED Count:- 1006', CAST(0x0000A43300D00895 AS DateTime), NULL)
INSERT [dbo].[DebugInfo] ([Id], [FileName], [Message], [Time], [UserId]) VALUES (5, N'\\172.16.1.18\datadrive\vol2\SCANNING\SCAN\NovToWin\SIP1\WH000003.txt', N'File Is LOCKED Count:- 254', CAST(0x0000A43300D00FC9 AS DateTime), NULL)
INSERT [dbo].[DebugInfo] ([Id], [FileName], [Message], [Time], [UserId]) VALUES (6, N'\\172.16.1.18\datadrive\vol2\SCANNING\SCAN\NovToWin\SIP1\WH000003.htm', N'File Is LOCKED Count:- 136', CAST(0x0000A43300D01001 AS DateTime), NULL)
INSERT [dbo].[DebugInfo] ([Id], [FileName], [Message], [Time], [UserId]) VALUES (7, N'\\172.16.1.18\datadrive\vol2\SCANNING\SCAN\NovToWin\SIP1\WH000004.txt', N'File Is LOCKED Count:- 772', CAST(0x0000A43300D0167E AS DateTime), NULL)
INSERT [dbo].[DebugInfo] ([Id], [FileName], [Message], [Time], [UserId]) VALUES (8, N'\\172.16.1.18\datadrive\vol2\SCANNING\SCAN\NovToWin\SIP1\WH000004.htm', N'File Is LOCKED Count:- 181', CAST(0x0000A43300D016C5 AS DateTime), NULL)
INSERT [dbo].[DebugInfo] ([Id], [FileName], [Message], [Time], [UserId]) VALUES (9, N'\\172.16.1.18\datadrive\vol2\SCANNING\SCAN\NovToWin\SIP1\WH000005.txt', N'File Is LOCKED Count:- 347', CAST(0x0000A43300D01B65 AS DateTime), NULL)
INSERT [dbo].[DebugInfo] ([Id], [FileName], [Message], [Time], [UserId]) VALUES (10, N'\\172.16.1.18\datadrive\vol2\SCANNING\SCAN\NovToWin\SIP1\WH000005.htm', N'File Is LOCKED Count:- 117', CAST(0x0000A43300D01B8F AS DateTime), NULL)
INSERT [dbo].[DebugInfo] ([Id], [FileName], [Message], [Time], [UserId]) VALUES (11, N'\\172.16.1.18\datadrive\vol2\SCANNING\SCAN\NovToWin\SIP1\WH000006.txt', N'File Is LOCKED Count:- 572', CAST(0x0000A43300F13506 AS DateTime), NULL)
INSERT [dbo].[DebugInfo] ([Id], [FileName], [Message], [Time], [UserId]) VALUES (12, N'\\172.16.1.18\datadrive\vol2\SCANNING\SCAN\NovToWin\SIP1\WH000006.htm', N'File Is LOCKED Count:- 562', CAST(0x0000A43300F13632 AS DateTime), NULL)
这是我用来生成RawFileName
的内容SELECT *, SUBSTRING((REVERSE
(
SUBSTRING
(
REVERSE(FileName),0,CHARINDEX('\',REVERSE(FileName))
)
)),0, CHARINDEX('.',REVERSE
(
SUBSTRING
(
REVERSE(FileName),0,CHARINDEX('\',REVERSE(FileName))
)
))) AS RawFileName from DebugInfo
由于
答案 0 :(得分:4)
这样做你想要的吗?
select avg(diff_ms * 1.0)
from (select datediff(millisecond, min(time), max(time)) as diff_ms
from t
group by rawfilename
) t;
答案 1 :(得分:1)
select avg(diff)
from ( select filename, DATEDIFF(MILLISECOND, Time, nextTime) as diff
from ( select [FileName]
, [Time]
, lead(Time, 1, 0) OVER (ORDER BY id) AS nextTime
, row_number() over (partition by FileName order by Time) as rn
from [DebugInfo]
) tt
where tt.rn = 1
) ttt
select avg(diff)
from ( select datediff(millisecond, minTime, maxTime) as diff
from ( select min(time) as minTime, max(time) ss maxTime
from [DebugInfo]
group by rawfilename
) tt
) ttt;