即使数据库中不存在我如何返回特定时间的数据
Posted
技术标签:
【中文标题】即使数据库中不存在我如何返回特定时间的数据【英文标题】:How can I return data for a specific time even if does't exist in db 【发布时间】:2016-08-23 14:24:49 【问题描述】:当日期时间范围之间的特定时间不存在于表中时,我想返回空值。例如:如果 2016-08-23 14:24:05 没有任何值,I_Data 应返回空值或零。请参阅下表和查询。 添加数据,由于时间段是2分钟,所以每组按列返回120行。
CREATE TABLE [dbo].[MData](
[MID] [varchar](50) NOT NULL,
[StartTime] [datetime] NOT NULL,
[SID] [int] NOT NULL,
[PID] [int] NOT NULL,
[I_Data] [decimal](10, 4) NOT NULL,
CONSTRAINT [PK_MData] PRIMARY KEY CLUSTERED
(
[MID] ASC,
[StartTime] ASC,
[SID] ASC,
[PID] 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
SELECT MID
,convert(varchar, StartTime, 120) AS StartTime
,SID
,PID
,Max(I_Data) AS MaxData
FROM MData where starttime between DATEADD(minute, -2, GETUTCDATE())
and GETUTCDATE()
GROUP BY MID, SID, PID, convert(varchar, StartTime, 120)
ORDER BY PID, convert(varchar, StartTime, 120);
GO
SET ANSI_PADDING OFF
GO
INSERT [dbo].[MData] ([MID], [StartTime], [SID], [PID], [I_Data]) VALUES (N'77', CAST(0x0000A66B0109E8D3 AS DateTime), 1, 1, CAST(1702.0000 AS Decimal(10, 4)))
INSERT [dbo].[MData] ([MID], [StartTime], [SID], [PID], [I_Data]) VALUES (N'77', CAST(0x0000A66B0109EA05 AS DateTime), 1, 2, CAST(1119.0000 AS Decimal(10, 4)))
INSERT [dbo].[MData] ([MID], [StartTime], [SID], [PID], [I_Data]) VALUES (N'77', CAST(0x0000A66B0109EA05 AS DateTime), 1, 3, CAST(1760.0000 AS Decimal(10, 4)))
INSERT [dbo].[MData] ([MID], [StartTime], [SID], [PID], [I_Data]) VALUES (N'77', CAST(0x0000A66B0109EA06 AS DateTime), 1, 4, CAST(1187.0000 AS Decimal(10, 4)))
INSERT [dbo].[MData] ([MID], [StartTime], [SID], [PID], [I_Data]) VALUES (N'77', CAST(0x0000A66B0109EA08 AS DateTime), 1, 5, CAST(1769.0000 AS Decimal(10, 4)))
INSERT [dbo].[MData] ([MID], [StartTime], [SID], [PID], [I_Data]) VALUES (N'77', CAST(0x0000A66B0109EA08 AS DateTime), 1, 6, CAST(1686.0000 AS Decimal(10, 4)))
INSERT [dbo].[MData] ([MID], [StartTime], [SID], [PID], [I_Data]) VALUES (N'77', CAST(0x0000A66B0109EA09 AS DateTime), 1, 7, CAST(1000.0000 AS Decimal(10, 4)))
INSERT [dbo].[MData] ([MID], [StartTime], [SID], [PID], [I_Data]) VALUES (N'77', CAST(0x0000A66B0109EA0A AS DateTime), 1, 8, CAST(152.0000 AS Decimal(10, 4)))
INSERT [dbo].[MData] ([MID], [StartTime], [SID], [PID], [I_Data]) VALUES (N'77', CAST(0x0000A66B0109EA2A AS DateTime), 1, 1, CAST(1688.0000 AS Decimal(10, 4)))
INSERT [dbo].[MData] ([MID], [StartTime], [SID], [PID], [I_Data]) VALUES (N'77', CAST(0x0000A66B0109EA2B AS DateTime), 1, 2, CAST(1550.0000 AS Decimal(10, 4)))
INSERT [dbo].[MData] ([MID], [StartTime], [SID], [PID], [I_Data]) VALUES (N'77', CAST(0x0000A66B0109EA2C AS DateTime), 1, 3, CAST(1897.0000 AS Decimal(10, 4)))
INSERT [dbo].[MData] ([MID], [StartTime], [SID], [PID], [I_Data]) VALUES (N'77', CAST(0x0000A66B0109EA2D AS DateTime), 1, 4, CAST(308.0000 AS Decimal(10, 4)))
INSERT [dbo].[MData] ([MID], [StartTime], [SID], [PID], [I_Data]) VALUES (N'77', CAST(0x0000A66B0109EA2E AS DateTime), 1, 5, CAST(1059.0000 AS Decimal(10, 4)))
INSERT [dbo].[MData] ([MID], [StartTime], [SID], [PID], [I_Data]) VALUES (N'77', CAST(0x0000A66B0109EA2F AS DateTime), 1, 6, CAST(1432.0000 AS Decimal(10, 4)))
INSERT [dbo].[MData] ([MID], [StartTime], [SID], [PID], [I_Data]) VALUES (N'77', CAST(0x0000A66B0109EA2F AS DateTime), 1, 7, CAST(176.0000 AS Decimal(10, 4)))
INSERT [dbo].[MData] ([MID], [StartTime], [SID], [PID], [I_Data]) VALUES (N'77', CAST(0x0000A66B0109EA30 AS DateTime), 1, 8, CAST(1994.0000 AS Decimal(10, 4)))
INSERT [dbo].[MData] ([MID], [StartTime], [SID], [PID], [I_Data]) VALUES (N'77', CAST(0x0000A66B0109EA31 AS DateTime), 2, 1, CAST(1901.0000 AS Decimal(10, 4)))
INSERT [dbo].[MData] ([MID], [StartTime], [SID], [PID], [I_Data]) VALUES (N'77', CAST(0x0000A66B0109EA32 AS DateTime), 2, 2, CAST(158.0000 AS Decimal(10, 4)))
INSERT [dbo].[MData] ([MID], [StartTime], [SID], [PID], [I_Data]) VALUES (N'77', CAST(0x0000A66B0109EA33 AS DateTime), 2, 3, CAST(1880.0000 AS Decimal(10, 4)))
INSERT [dbo].[MData] ([MID], [StartTime], [SID], [PID], [I_Data]) VALUES (N'77', CAST(0x0000A66B0109EA58 AS DateTime), 1, 1, CAST(1612.0000 AS Decimal(10, 4)))
INSERT [dbo].[MData] ([MID], [StartTime], [SID], [PID], [I_Data]) VALUES (N'77', CAST(0x0000A66B0109EA59 AS DateTime), 1, 2, CAST(859.0000 AS Decimal(10, 4)))
INSERT [dbo].[MData] ([MID], [StartTime], [SID], [PID], [I_Data]) VALUES (N'77', CAST(0x0000A66B0109EA5A AS DateTime), 1, 3, CAST(1214.0000 AS Decimal(10, 4)))
INSERT [dbo].[MData] ([MID], [StartTime], [SID], [PID], [I_Data]) VALUES (N'77', CAST(0x0000A66B0109EA5B AS DateTime), 1, 4, CAST(595.0000 AS Decimal(10, 4)))
INSERT [dbo].[MData] ([MID], [StartTime], [SID], [PID], [I_Data]) VALUES (N'77', CAST(0x0000A66B0109EA5C AS DateTime), 1, 5, CAST(127.0000 AS Decimal(10, 4)))
INSERT [dbo].[MData] ([MID], [StartTime], [SID], [PID], [I_Data]) VALUES (N'77', CAST(0x0000A66B0109EA5D AS DateTime), 1, 6, CAST(1805.0000 AS Decimal(10, 4)))
INSERT [dbo].[MData] ([MID], [StartTime], [SID], [PID], [I_Data]) VALUES (N'77', CAST(0x0000A66B0109EA5E AS DateTime), 1, 7, CAST(1351.0000 AS Decimal(10, 4)))
INSERT [dbo].[MData] ([MID], [StartTime], [SID], [PID], [I_Data]) VALUES (N'77', CAST(0x0000A66B0109EA5F AS DateTime), 1, 8, CAST(1154.0000 AS Decimal(10, 4)))
【问题讨论】:
您需要详细说明您的问题并提供示例数据集和预期输出。如果没有符合 where 条件的行,您的 where 语句将返回 NULL。 您将首先使用您的时间间隔创建一个关系(表、子查询、CTE 等),然后将其与您在每个间隔上的数据 LEFT JOIN。 添加了示例数据。 【参考方案1】:试试下面的代码。
SELECT m1.MID
,convert(VARCHAR, m1.StartTime, 120) AS StartTime
,m1.SID
,m1.PID
,Max(m2.I_Data) AS MaxData
FROM MData m1
LEFT JOIN MData m2
ON m1.MID=m2.MID AND m1.SID=m2.SID AND m1.PID=m2.PID
AND m1.starttime between DATEADD(minute, -2, GETUTCDATE()) and GETUTCDATE()
GROUP BY m1.MID, m1.SID, m1.PID, convert(VARCHAR, m1.StartTime, 120)
ORDER BY m1.PID, convert(VARCHAR, m1.StartTime, 120);
【讨论】:
感谢您的回复,这是获取值,但是对于一个 mid,sid,pid 组 120 记录应该在那里 120 条记录..但是如何?您能否解释一下您的预期结果是什么.. 或给出您想要的示例结果集.. 每一秒都应该有一条记录,如果它存在于表中,它应该返回 null 或零。在上面的查询时间范围是两分钟,所以应该记录所有 120 秒以上是关于即使数据库中不存在我如何返回特定时间的数据的主要内容,如果未能解决你的问题,请参考以下文章