Hope you are still monitoring this, Erland...
I have made several changes to my database and now need to track each column's time. I can do them individually with your code, but can't figure out how to iterate through the rest of the columns.
Here is my example database:
[code]
Create Table MachineDataTest(
Machine nvarchar(256),
InCycle numeric(18,0),
R1 numeric(18,0),
R2 numeric(18,0),
R3 numeric(18,0),
R4 numeric(18,0),
R5 numeric(18,0),
Uncategorized numeric(18,0),
Timestamp datetime Default GetUTCDate(),
id int Identity(1,1) Primary Key)
Insert Into MachineDataTest(Machine,InCycle,R1,R2,R3,R4,R5,Uncategorized)
Values ('LH35N-3000B', 1,0,0,0,0,0,0)
WaitFor Delay '00:00:02'
Insert Into MachineDataTest(Machine,InCycle,R1,R2,R3,R4,R5,Uncategorized)
Values ('LH35N-3000B', 0,0,0,0,0,0,1)
WaitFor Delay '00:00:02'
Insert Into MachineDataTest(Machine,InCycle,R1,R2,R3,R4,R5,Uncategorized)
Values ('LH35N-3000B', 1,0,0,0,0,0,0)
WaitFor Delay '00:00:02'
Insert Into MachineDataTest(Machine,InCycle,R1,R2,R3,R4,R5,Uncategorized)
Values ('LH35N-3000B', 0,1,0,0,0,0,0)
WaitFor Delay '00:00:02'
Insert Into MachineDataTest(Machine,InCycle,R1,R2,R3,R4,R5,Uncategorized)
Values ('LH35N-3000B', 0,0,0,0,0,1,0)
WaitFor Delay '00:00:02'
Insert Into MachineDataTest(Machine,InCycle,R1,R2,R3,R4,R5,Uncategorized)
Values ('LH35N-3000B', 0,0,1,0,0,0,0)
WaitFor Delay '00:00:02'
Insert Into MachineDataTest(Machine,InCycle,R1,R2,R3,R4,R5,Uncategorized)
Values ('LH35N-3000B', 0,0,0,1,0,0,0)
WaitFor Delay '00:00:02'
Insert Into MachineDataTest(Machine,InCycle,R1,R2,R3,R4,R5,Uncategorized)
Values ('LH35N-3000B', 0,0,0,0,1,0,0)
WaitFor Delay '00:00:02'
Insert Into MachineDataTest(Machine,InCycle,R1,R2,R3,R4,R5,Uncategorized)
Values ('LH35N-3000B', 1,0,0,1,0,0,0)
Insert Into MachineDataTest(Machine,InCycle,R1,R2,R3,R4,R5,Uncategorized)
Values ('LH35N-3000B', 1,0,0,0,0,0,0)
WaitFor Delay '00:00:02'
Insert Into MachineDataTest(Machine,InCycle,R1,R2,R3,R4,R5,Uncategorized)
Values ('LH35N-3000B', 0,0,0,0,0,0,1)
WaitFor Delay '00:00:02'
Insert Into MachineDataTest(Machine,InCycle,R1,R2,R3,R4,R5,Uncategorized)
Values ('LH35N-3000B', 1,0,0,0,0,0,0)
WaitFor Delay '00:00:02'
Insert Into MachineDataTest(Machine,InCycle,R1,R2,R3,R4,R5,Uncategorized)
Values ('LH35N-3000B', 0,1,0,0,0,0,0)
WaitFor Delay '00:00:02'
Insert Into MachineDataTest(Machine,InCycle,R1,R2,R3,R4,R5,Uncategorized)
Values ('LH35N-3000B', 0,0,0,0,0,1,0)
WaitFor Delay '00:00:02'
Insert Into MachineDataTest(Machine,InCycle,R1,R2,R3,R4,R5,Uncategorized)
Values ('LH35N-3000B', 0,0,1,0,0,0,0)
WaitFor Delay '00:00:02'
Insert Into MachineDataTest(Machine,InCycle,R1,R2,R3,R4,R5,Uncategorized)
Values ('LH35N-3000B', 0,0,0,1,0,0,0)
WaitFor Delay '00:00:02'
Insert Into MachineDataTest(Machine,InCycle,R1,R2,R3,R4,R5,Uncategorized)
Values ('LH35N-3000B', 0,0,0,0,1,0,0)
WaitFor Delay '00:00:02'
Insert Into MachineDataTest(Machine,InCycle,R1,R2,R3,R4,R5,Uncategorized)
Values ('LH35N-3000B', 1,0,0,1,0,0,0)
[/code]
Here is the code that I used for InCycle time:
WITH InCycles AS
(SELECT Machine,
CASE WHEN InCycle = 1
THEN datediff(ss, Timestamp,
LEAD(Timestamp, 1, sysutcdatetime())
OVER (PARTITION BY Machine ORDER BY Timestamp))
ELSE 0
END AS InCycle
FROM MachineData Where Timestamp >= DATEADD(hour, -24, sysutcdatetime()))
SELECT SUM(InCycle) as InCycle FROM InCycles where Machine = 'LH35N-3000B';
I need to see (all times are examples):
Reason Time(in Seconds)
InCycle 67
R1 50
R2 39
R3 27
R4 20
R5 14
Uncategorized 2
I'm still using SQL server 2014.
Thanks,
Mike