Hello,
I need some tips/solution on creating an SQL statement that selects each row(record) in a table when a particular column value changes.
Some code follows to create an example table and also to insert some basic data with an example of the output that I need to achieve.
(This is for a Roster System I am developing, Each row/record is a 15min time inteval representing the product being manufactured at that time and the employee resource requirement during that time interval. I need to identify the datetime when a new product is scheduled to begin manufacture and the required employee resource at that point in time. I've provided a very cut down simplistic data example but its enough to show what I require).
I NEED SOME T-SQL that lists the first record when the number of employees required changes (Emp_ReqNo changes - either more or less) to achieve a list from the above table as per below:
Thank You Kieran
I need some tips/solution on creating an SQL statement that selects each row(record) in a table when a particular column value changes.
Some code follows to create an example table and also to insert some basic data with an example of the output that I need to achieve.
(This is for a Roster System I am developing, Each row/record is a 15min time inteval representing the product being manufactured at that time and the employee resource requirement during that time interval. I need to identify the datetime when a new product is scheduled to begin manufacture and the required employee resource at that point in time. I've provided a very cut down simplistic data example but its enough to show what I require).
Code:
CREATE TABLE [dbo].[Rstr_Requirements](
[RstrReq_ID] [INT] IDENTITY(1,1) PRIMARY KEY,
[IntervalMins] [int] NULL,
[IntervalNo] [int] NULL,
[IntervalStartDT] [DateTime] NULL,
[IntervalEndDT] [DateTime] NULL,
[ProductCode] [NCHAR](10) NULL,
[prodtSchdlStartDT] [datetime] NULL,
[prodtSchdlEndDT] [datetime] NULL,
[Emp_ReqNo] [int] NULL)
ON [PRIMARY]
INSERT INTO [dbo].[Rstr_Requirements]
(IntervalMins, IntervalNo, IntervalStartDT, IntervalEndDT, ProductCode,
prodtSchdlStartDT, prodtSchdlEndDT,Emp_ReqNo)
VALUES
(15, 0, '2009-10-05 00:00:00.000','2009-10-05 00:15:00.000', NULL,
NULL, NULL, NULL)
INSERT INTO [dbo].[Rstr_Requirements]
(IntervalMins, IntervalNo, IntervalStartDT, IntervalEndDT, ProductCode,
prodtSchdlStartDT, prodtSchdlEndDT,Emp_ReqNo)
VALUES
(15, 1, '2009-10-05 00:15:00.000','2009-10-05 00:30:00.000', NULL,
NULL, NULL, NULL)
INSERT INTO [dbo].[Rstr_Requirements]
(IntervalMins, IntervalNo, IntervalStartDT, IntervalEndDT, ProductCode,
prodtSchdlStartDT, prodtSchdlEndDT,Emp_ReqNo)
VALUES
(15, 2, '2009-10-05 00:30:00.000','2009-10-05 00:45:00.000', 'PRODA',
'2009-10-05 00:30:00.000', '2009-10-05 01:00:00.000', 10)
INSERT INTO [dbo].[Rstr_Requirements]
(IntervalMins, IntervalNo, IntervalStartDT, IntervalEndDT, ProductCode,
prodtSchdlStartDT, prodtSchdlEndDT,Emp_ReqNo)
VALUES
(15, 3, '2009-10-05 00:45:00.000','2009-10-05 01:00:00.000', 'PRODA',
'2009-10-05 00:30:00.000', '2009-10-05 01:00:00.000', 10)
INSERT INTO [dbo].[Rstr_Requirements]
(IntervalMins, IntervalNo, IntervalStartDT, IntervalEndDT, ProductCode,
prodtSchdlStartDT, prodtSchdlEndDT,Emp_ReqNo)
VALUES
(15, 4, '2009-10-05 01:00:00.000','2009-10-05 01:15:00.000', 'PRODC',
'2009-10-05 01:00:00.000', '2009-10-05 01:15:00.000', 25)
INSERT INTO [dbo].[Rstr_Requirements]
(IntervalMins, IntervalNo, IntervalStartDT, IntervalEndDT, ProductCode,
prodtSchdlStartDT, prodtSchdlEndDT,Emp_ReqNo)
VALUES
(15, 5, '2009-10-05 01:15:00.000','2009-10-05 01:30:00.000', 'PRODC',
'2009-10-05 00:30:00.000', '2009-10-05 01:00:00.000', 25)
INSERT INTO [dbo].[Rstr_Requirements]
(IntervalMins, IntervalNo, IntervalStartDT, IntervalEndDT, ProductCode,
prodtSchdlStartDT, prodtSchdlEndDT,Emp_ReqNo)
VALUES
(15, 6, '2009-10-05 01:30:00.000','2009-10-05 01:45:00.000', 'PRODC',
'2009-10-05 00:30:00.000', '2009-10-05 01:00:00.000', 25)
INSERT INTO [dbo].[Rstr_Requirements]
(IntervalMins, IntervalNo, IntervalStartDT, IntervalEndDT, ProductCode,
prodtSchdlStartDT, prodtSchdlEndDT,Emp_ReqNo)
VALUES
(15, 7, '2009-10-05 01:45:00.000','2009-10-05 02:00:00.000', 'PRODB',
'2009-10-05 01:45:00.000', '2009-10-05 02:00:00.000', 5)
INSERT INTO [dbo].[Rstr_Requirements]
(IntervalMins, IntervalNo, IntervalStartDT, IntervalEndDT, ProductCode,
prodtSchdlStartDT, prodtSchdlEndDT,Emp_ReqNo)
VALUES
(15, 8, '2009-10-05 00:00:00.000','2009-10-05 00:15:00.000', NULL,
NULL, NULL, NULL)
I NEED SOME T-SQL that lists the first record when the number of employees required changes (Emp_ReqNo changes - either more or less) to achieve a list from the above table as per below:
Code:
ProductCode, ProductStartDT, EmpReq, Length (Mins), Start(IntervalNo)
PRODTA, 2009-10-05 00:30:00.000, 10 ,30 2
PRODTC, 2009-10-05 01:00:00.000, 25 ,45 4
PRODTD, 2009-10-05 01:45:00.000, 05 ,15 7
Thank You Kieran