Let me try to clarify. I am not sure how to provide consumable data. At the bottom is a sql script I created.
The result I am looking for is as follows:
Each ProgramKPIId will have one or more DataSetNo
Identify the DataSetNo based on the maximum value for ProgramKPIMasterId = -1 for each ProgramKPId.
In the test data above for ProgramKPIId =2, the maximum value for ProgramKPIMasterId = -1 is 10/11/2024. The corresponding DataSetNo = 1.
The corresponding value for ProgramKPIId = 2 AND ProgramKPIMasterId = 7 AND DataSetNo = 1 would be 80.
The result I am looking for is 80.
Any suggestions to improve my posts would be appreciated.
Thank you.
Rob
SQL Script for data
CREATE TABLE [dbo].[ProgramKPISInfo](
[ProgramKPISInfoId] [bigint] IDENTITY(1,1) NOT NULL,
[ProgramKPIId] [bigint] NOT NULL,
[DataSetNo] [bigint] NOT NULL,
[ProgramKPIMasterId] [bigint] NOT NULL,
[value] nvarchar NULL,
[CreatedByUserId] [bigint] NULL,
[CreatedUTCDate] [datetime] NULL,
[ModifiedByUserId] [bigint] NULL,
[ModifiedUTCDate] [datetime] NULL,
PRIMARY KEY CLUSTERED
(
[ProgramKPISInfoId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
SET IDENTITY_INSERT [dbo].[ProgramKPISInfo] ON
INSERT [dbo].[ProgramKPISInfo] ([ProgramKPISInfoId], [ProgramKPIId], [DataSetNo], [ProgramKPIMasterId], [value], [CreatedByUserId], [CreatedUTCDate], [ModifiedByUserId], [ModifiedUTCDate]) VALUES (6, 2, 1, 0, N'Test2', 105, CAST(N'2023-10-12T15:08:59.987' AS DateTime), NULL, NULL)
INSERT [dbo].[ProgramKPISInfo] ([ProgramKPISInfoId], [ProgramKPIId], [DataSetNo], [ProgramKPIMasterId], [value], [CreatedByUserId], [CreatedUTCDate], [ModifiedByUserId], [ModifiedUTCDate]) VALUES (7, 2, 1, -1, N'10/12/2023 12:00:00 AM', 105, CAST(N'2023-10-12T15:08:59.987' AS DateTime), NULL, NULL)
INSERT [dbo].[ProgramKPISInfo] ([ProgramKPISInfoId], [ProgramKPIId], [DataSetNo], [ProgramKPIMasterId], [value], [CreatedByUserId], [CreatedUTCDate], [ModifiedByUserId], [ModifiedUTCDate]) VALUES (8, 2, 1, 12, N'Yellow', 105, CAST(N'2023-10-12T15:08:59.987' AS DateTime), NULL, NULL)
INSERT [dbo].[ProgramKPISInfo] ([ProgramKPISInfoId], [ProgramKPIId], [DataSetNo], [ProgramKPIMasterId], [value], [CreatedByUserId], [CreatedUTCDate], [ModifiedByUserId], [ModifiedUTCDate]) VALUES (9, 2, 1, 11, N'Yellow', 105, CAST(N'2023-10-12T15:08:59.987' AS DateTime), NULL, NULL)
INSERT [dbo].[ProgramKPISInfo] ([ProgramKPISInfoId], [ProgramKPIId], [DataSetNo], [ProgramKPIMasterId], [value], [CreatedByUserId], [CreatedUTCDate], [ModifiedByUserId], [ModifiedUTCDate]) VALUES (10, 2, 1, 9, N'100', 105, CAST(N'2023-10-12T15:08:59.987' AS DateTime), NULL, NULL)
INSERT [dbo].[ProgramKPISInfo] ([ProgramKPISInfoId], [ProgramKPIId], [DataSetNo], [ProgramKPIMasterId], [value], [CreatedByUserId], [CreatedUTCDate], [ModifiedByUserId], [ModifiedUTCDate]) VALUES (11, 2, 1, 7, N'100.00', 105, CAST(N'2023-10-12T15:08:59.987' AS DateTime), NULL, NULL)
INSERT [dbo].[ProgramKPISInfo] ([ProgramKPISInfoId], [ProgramKPIId], [DataSetNo], [ProgramKPIMasterId], [value], [CreatedByUserId], [CreatedUTCDate], [ModifiedByUserId], [ModifiedUTCDate]) VALUES (12, 2, 1, 10, N'Green', 105, CAST(N'2023-10-12T15:08:59.987' AS DateTime), NULL, NULL)
INSERT [dbo].[ProgramKPISInfo] ([ProgramKPISInfoId], [ProgramKPIId], [DataSetNo], [ProgramKPIMasterId], [value], [CreatedByUserId], [CreatedUTCDate], [ModifiedByUserId], [ModifiedUTCDate]) VALUES (13, 2, 2, 0, N'Test3', 105, CAST(N'2023-10-12T15:09:30.953' AS DateTime), NULL, NULL)
INSERT [dbo].[ProgramKPISInfo] ([ProgramKPISInfoId], [ProgramKPIId], [DataSetNo], [ProgramKPIMasterId], [value], [CreatedByUserId], [CreatedUTCDate], [ModifiedByUserId], [ModifiedUTCDate]) VALUES (14, 2, 2, -1, N'10/12/2023 12:00:00 AM', 105, CAST(N'2023-10-12T15:09:30.953' AS DateTime), NULL, NULL)
INSERT [dbo].[ProgramKPISInfo] ([ProgramKPISInfoId], [ProgramKPIId], [DataSetNo], [ProgramKPIMasterId], [value], [CreatedByUserId], [CreatedUTCDate], [ModifiedByUserId], [ModifiedUTCDate]) VALUES (15, 2, 2, 12, N'Green', 105, CAST(N'2023-10-12T15:09:30.953' AS DateTime), NULL, NULL)
INSERT [dbo].[ProgramKPISInfo] ([ProgramKPISInfoId], [ProgramKPIId], [DataSetNo], [ProgramKPIMasterId], [value], [CreatedByUserId], [CreatedUTCDate], [ModifiedByUserId], [ModifiedUTCDate]) VALUES (16, 2, 2, 11, N'Green', 105, CAST(N'2023-10-12T15:09:30.953' AS DateTime), NULL, NULL)
INSERT [dbo].[ProgramKPISInfo] ([ProgramKPISInfoId], [ProgramKPIId], [DataSetNo], [ProgramKPIMasterId], [value], [CreatedByUserId], [CreatedUTCDate], [ModifiedByUserId], [ModifiedUTCDate]) VALUES (17, 2, 2, 9, N'100', 105, CAST(N'2023-10-12T15:09:30.953' AS DateTime), NULL, NULL)
INSERT [dbo].[ProgramKPISInfo] ([ProgramKPISInfoId], [ProgramKPIId], [DataSetNo], [ProgramKPIMasterId], [value], [CreatedByUserId], [CreatedUTCDate], [ModifiedByUserId], [ModifiedUTCDate]) VALUES (18, 2, 2, 7, N'100.00', 105, CAST(N'2023-10-12T15:09:30.953' AS DateTime), NULL, NULL)
INSERT [dbo].[ProgramKPISInfo] ([ProgramKPISInfoId], [ProgramKPIId], [DataSetNo], [ProgramKPIMasterId], [value], [CreatedByUserId], [CreatedUTCDate], [ModifiedByUserId], [ModifiedUTCDate]) VALUES (19, 2, 2, 10, N'Green', 105, CAST(N'2023-10-12T15:09:30.953' AS DateTime), NULL, NULL)
SET IDENTITY_INSERT [dbo].[ProgramKPISInfo] OFF
GO
ALTER TABLE [dbo].[ProgramKPISInfo] WITH CHECK ADD FOREIGN KEY([CreatedByUserId])
REFERENCES [dbo].[User] ([UserId])
GO
ALTER TABLE [dbo].[ProgramKPISInfo] WITH CHECK ADD FOREIGN KEY([ModifiedByUserId])
REFERENCES [dbo].[User] ([UserId])
GO