所以我有一个CTE可以创建下面的结果。这是一个批处理运行的例子,批处理经历的阶段以及它们各自的开始和结束时间。我想要完成的是获取批处理运行期间收集的数据,并在数据进入表格的阶段之间插入。
rownum PhaseName PhaseID Time TimeEnd
1 Air Removal 2 2016-06-01 11:46:46.000 2016-06-01 11:56:47.000
2 Heat up 3 2016-06-01 11:56:47.000 2016-06-01 12:06:17.000
3 Steriliztion 4 2016-06-01 12:06:17.000 2016-06-01 12:23:40.000
4 Air Pressurization 5 2016-06-01 12:23:40.000 2016-06-01 12:27:18.000
5 Exhaust 6 2016-06-01 12:27:18.000 2016-06-01 12:29:35.000
6 Drying 7 2016-06-01 12:29:35.000 2016-06-01 12:34:36.000
7 Equalization 8 2016-06-01 12:34:36.000 2016-06-01 12:34:36.000
让我们假设一个数据点带有数据:
Chamber Pressure 2016-06-03 11:47:23.000
Chamber Temperature 2016-06-03 12:10:24.000
我想将这些数据置于其收集的阶段。所以第一个数据点是在空气清除下,第二个是在灭菌下。
原因是,我想在我的SSRS中使用阶段名称作为父组。但是我的代码现在倾向于创建漏洞,而完整的连接会创建大量不需要的数据,而这些数据是我使用SSRS中的过滤器进行的。
CODE:
;with ss (EquipmentTag, Name) as(
Select 'Equipment Tags_IO_RedirectAI_udt{0}_Out_real', '(PIT112) Chamber Pressure' Union
Select 'Equipment Tags_IO_RedirectAI_udt{1}_Out_real','(TT113) Drain Temperaure' Union
Select 'Equipment Tags_IO_RedirectAI_udt{2}_Out_real','(TT205) Jacket Temperature' Union
Select 'Equipment Tags_IO_RedirectAI_udt{3}_Out_real','(TT114) Filter Temperature' Union
Select 'Equipment Tags_IO_RedirectAI_udt{4}_Out_real','(TT405) Waste Water Temperature' Union
Select 'Equipment Tags_IO_RedirectAI_udt{7}_Out_real','(TT401) Vacuum Pump Seal Water Temperature' Union
Select 'Equipment Tags_IO_RedirectAI_udt{8}_Out_real','(TT115.1) Chamber Temperaure' Union
Select 'Equipment Tags_PID{0}_Output','Chamber Temperature PID Output' Union
Select 'Equipment Tags_PID{1}_Output','Drain Temperature PID Output' Union
Select 'Equipment Tags_PID{2}_Output','Filter Temperature PID Output' Union
Select 'Equipment Tags_PID{3}_Output','Jacket Temperature PID Output' Union
Select 'Equipment Tags_PID{4}_Output','Jacket Cooling Water PID Output' Union
Select 'Equipment Tags_PID{0}_Setpoint','Chamber Temperature PID Setpoint' Union
Select 'Equipment Tags_PID{1}_Setpoint','Drain Temperature PID Setpoint' Union
Select 'Equipment Tags_PID{2}_Setpoint','Filter Temperature PID Setpoint' Union
Select 'Equipment Tags_PID{3}_Setpoint','Jacket Temperature PID Setpoint' Union
Select 'Equipment Tags_PID{4}_Setpoint','Jacket Cooling Water PID Setpoint' Union
Select 'PID_Chamber_PIC_CtrlParamsBackUp_Gain','Chamber Temperature PID Proportional Gain' Union
Select 'PID_Chamber_PIC_CtrlParamsBackUp_Td','Chamber Temperature PID Differential Gain' Union
Select 'PID_Chamber_PIC_CtrlParamsBackUp_Ti','Chamber Temperature PID Integral Gain' Union
Select 'PID_Drain_TIC_CtrlParamsBackUp_Gain','Drain Temperature PID Proportional Gain' Union
Select 'PID_Drain_TIC_CtrlParamsBackUp_Td','Drain Temperature PID Differential Gain' Union
Select 'PID_Drain_TIC_CtrlParamsBackUp_Ti','Drain Temperature PID Integral Gain' Union
Select 'PID_Filter_TIC_CtrlParamsBackUp_Gain','Filter Temperature PID Proportional Gain' Union
Select 'PID_Filter_TIC_CtrlParamsBackUp_Td','Filter Temperature PID Differential Gain' Union
Select 'PID_Filter_TIC_CtrlParamsBackUp_Ti','Filter Temperature PID Integral Gain' Union
Select 'PID_Jacket_TIC_CtrlParamsBackUp_Gain','Jacket Temperature PID Proportional Gain' Union
Select 'PID_Jacket_TIC_CtrlParamsBackUp_Td','Jacket Temperature PID Differential Gain' Union
Select 'PID_Jacket_TIC_CtrlParamsBackUp_Ti','Jacket Temperature PID Integral Gain' Union
Select 'PID_Jacket_Cooling_Water_CtrlParamsBackUp_Gain','Jacket Temperature PID Proportional Gain' Union
Select 'PID_Jacket_Cooling_Water_CtrlParamsBackUp_Gain','Jacket Cooling Water Temperature PID Proportional Gain' Union
Select 'PID_Jacket_Cooling_Water_CtrlParamsBackUp_Td','Jacket Cooling Water Temperature PID Differential Gain' Union
Select 'PID_Jacket_Cooling_Water_CtrlParamsBackUp_Ti','Jacket Cooling Water Temperature PID Integral Gain' union
Select 'Controller Tags_PhaseID' , 'PhaseID'),
ss3(pkid,PhaseName) as(select 0, '' union
select '1', 'Setup' union
select '2', 'Air Removal' union
select '3', 'Heat up' union
select '4', 'Sterilization' union
select '5', 'Air Pressurization' union
select '6', 'Exhaust' union
select '7', 'Drying' union
select '8', 'Equalization' union
select '17', 'Air Pressure Test' union
select '18', 'Filter Sterilization' union
select '19', 'Air Leak Test'),
ss2 as (
select a.Name, Cast(TimeString as DateTime) as Time, cast(VarValue as float) as Value from Datalog0 as e left join ss as a on
e.VarName = a.Name where a.Name <> 'PhaseID'--)
union all
select PhaseName,Cast(TimeString as DateTime) as Time, cast(b.VarValue as float) as Value
from ss3 d inner join SiemensArchive b on b.VarValue = d.pkid where b.VarName = 'PhaseActive'),
ss4 as ( Select i.Name,i.Time, i.Value, PhaseName from ss2 i full join ss3 j on cast(j.pkid as decimal) = i.value),
ss6 as(select distinct row_number() over(order by value asc) as rownum, PhaseName, value as PhaseID,
Time from ss4 where PhaseName <> '' and value <> 0),
ss7 as(select i.rownum,i.PhaseName,i.PhaseID, i.time as TimeStart, lead(i.time) over(order by Time) as TimeEnd from ss6 as i)
--select * from ss7
select distinct coalesce(a.PhaseName,'Charge Gasket') as Phase,a.TimeStart,
A.Timeend, b.Name,b.Value,b.time from ss7 a full join ss2 b on a.Timestart <= b.Time and a.timeend >= b.time
and b.name <> 'Air Removal' and b.name <> 'Heat Up' and b.Name <> 'Sterilization' and b.name <> 'Filter Sterilization'
and b.name <> 'Air Pressurization' and b.name <> 'Equalization' and b.name <> 'Charge Gasket'
and a.PhaseName <> b.Name and b.Name <> 'Exhaust' and b.Name <> 'Drying'
group by timestart,phasename,timeend,name,value,time
order by timestart,time,timeend
DataLog0:
Equipment Tags_PID_CV_real{1} 6/1/2016 11:56:28 100 1 42522497543.4375
Equipment Tags_PID_CV_real{2} 6/1/2016 11:56:28 0 1 42522497543.4375
Equipment Tags_PID_CV_real{4} 6/1/2016 11:56:28 0 1 42522497543.4375
Equipment Tags_PID_CV_real{3} 6/1/2016 11:56:28 0 1 42522497543.4375
Equipment Tags_PID{0}_SetPoint 6/1/2016 11:56:28 29 1 42522497543.4375
PhaseActive 6/1/2016 11:56:47 3 1 42522497763.5069
Equipment Tags_IO_RedirectAI_udt{0}_Out_real 6/1/2016 11:57:28 23.3567390441895 1 42522498237.9282
Equipment Tags_IO_RedirectAI_udt{8}_Out_real 6/1/2016 11:57:28 112.375122070313 1 42522498237.9282
Equipment Tags_PID{1}_SetPoint 6/1/2016 11:57:28 121.099998474121 1 42522498237.9282
Equipment Tags_PID{2}_SetPoint 6/1/2016 11:57:28 122.5 1 42522498237.9282
Equipment Tags_PID{4}_SetPoint 6/1/2016 11:57:28 0 1 42522498237.9282
Equipment Tags_PID{3}_SetPoint 6/1/2016 11:57:28 119.800003051758 1 42522498237.9282
Equipment Tags_IO_RedirectAI_udt{3}_Out_real 6/1/2016 11:57:28 26.4351387023926 1 42522498237.9282
Equipment Tags_IO_RedirectAI_udt{2}_Out_real 6/1/2016 11:57:28 120.220428466797 1 42522498237.9282
Equipment Tags_IO_RedirectAI_udt{4}_Out_real 6/1/2016 11:57:28 57.4743041992188 1 42522498237.9282
Equipment Tags_IO_RedirectAI_udt{7}_Out_real 6/1/2016 11:57:28 31.2780513763428 1 42522498237.9282
Equipment Tags_IO_RedirectAI_udt{1}_Out_real 6/1/2016 11:57:28 111.593315124512 1 42522498237.9282
Equipment Tags_PID_CV_real{0} 6/1/2016 11:57:28 31.2675533294678 1 42522498237.9282
Equipment Tags_PID_CV_real{1} 6/1/2016 11:57:28 30.1672401428223 1 42522498237.9282
Equipment Tags_PID_CV_real{2} 6/1/2016 11:57:28 0 1 42522498237.9282
Equipment Tags_PID_CV_real{4} 6/1/2016 11:57:28 0 1 42522498237.9282
Equipment Tags_PID_CV_real{3} 6/1/2016 11:57:28 0 1 42522498237.9282
答案 0 :(得分:0)
已更新解决方案:
WITH FirstCTE
AS (
SELECT PhaseName, PhaseId, TimeStart FROM ...
), SecondCTE
AS (
SELECT PhaseName, PhaseId, TimeStart FROM ...
), AllResults
AS (
SELECT * FROM FirstCTE
UNION ALL
SELECT * FROM SecondCTE
)
SELECT ROW_NUMBER() OVER(ORDER BY a.TimeStart) AS RowNum, *, LEAD(a.TimeStart) OVER(ORDER BY a.TimeStart) AS TimeEnd
FROM AllResults a
ORDER BY RowNum