即使db中不存在,如何返回特定时间的数据

时间:2016-08-23 14:24:49

标签: sql sql-server tsql

我希望在表格中不存在日期时间范围之间的特定时间时返回空值。例如:如果2016-08-23 14:24:05没有任何值,I_Data应返回null或0。请参阅下面的表格和查询。 添加数据,因为时间段是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)))

1 个答案:

答案 0 :(得分:0)

尝试使用以下代码。

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);