日付の時間範囲の間の特定の時間が存在しない場合、NULL値を返すようにしたいとします。if 2016-08-23 14:24:05には値がありません。I_Dataはnullまたはゼロを返す必要があります。下記の表と質問をご覧ください。 データを追加しました。期間は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を返します。 – scsimon
まず、時間間隔を指定してリレーション(テーブル、サブクエリ、CTEなど)を作成し、各間隔でデータをLEFT JOINします。 –
サンプルデータが追加されました。 – User007