我有一个冗长且非常重复的查询,我需要每 5 分钟运行一次以进行报告,下面有几个 select 语句的摘录,我有 26 个与此类似的列需要总共运行 6 行不同的行,每行都有 7 个不同的 where 语句,基于时间框架、作业和运算符,所以与其创建大量的 T-SQL,并且每五分钟运行一次我想尝试创建一个存储过程,其中传递给它的参数是两位数的行号(在下面的示例中为“05”)、where 条件和列的标识符(在下面的示例中为“作业”)。我遇到的问题是连接 select 语句中的列名,我尝试了许多方法,但都没有奏效。
示例(是的,我知道很多转换为十进制,我想对自己大喊大叫,因为一切都是 varchar,但是将数据从我们的制造机器传输到 Db 的应用程序只会写为时间戳的字符串或日期时间,而 charindex 是因为我看到数据被放到小数点后 20 位,在极少数情况下甚至更远)
,AVG(CAST(LEFT("E05UP", (CHARINDEX(',', E05UP, 5) + 6)) AS decimal(11,6)))*100 AS JobE05Up
,AVG(CAST(LEFT(E05QUAL, (CHARINDEX(',', E05QUAL, 5) + 6)) AS decimal(11,6)))*100 AS JobE05Qual
,CASE WHEN AVG(CAST(LEFT(E05UP, (CHARINDEX(',', E05UP, 5) + 6)) AS decimal(11,6)))>0 AND AVG(CAST(LEFT(E05SPD, (CHARINDEX(',', E05SPD, 5) + 6)) AS decimal(11,6)))>0 AND
AVG(CAST(LEFT(E05QUAL, (CHARINDEX(',', E05QUAL, 5) + 6)) AS decimal(11,6)))>0 THEN (AVG(CAST(LEFT(E05UP, (CHARINDEX(',', E05UP, 5) + 6)) AS decimal(11,6)))*
(AVG(CAST(LEFT(E05SPD, (CHARINDEX(',', E05Spd, 5) + 6)) AS decimal(11,6)))/@E05SpeedMAX)*AVG(CAST(LEFT(E05QUAL, (CHARINDEX(',', E05QUAL, 5) + 6)) AS decimal(11,6)))*100)
ELSE 0 END AS JobE05OEE
简而言之,我想做的是:
SELECT AVG(E+@P1+SPD) AS @P2+Speed FROM Test.Dbo.Line+@P1 WHERE @P3
编辑:整个查询从最后一个 Db 只写中选择数据,在将所有 Casts 更改为一个函数之后,唯一会在此报告和此报告的所有其他查询之间更改的是 Line # (L05) the Column Name, where 子句,以及被求和或平均的数字,因为我们将使用不止一行。
DECLARE @L05CurWO as INT
DECLARE @L05CurCount AS INT
DECLARE @L05SpeedMAX AS INT
DECLARE @E05SpeedMAX AS INT
DECLARE @H05SpeedMAX AS INT
DECLARE @P05SpeedMAX AS INT
DECLARE @C05SpeedMAX AS INT
DECLARE @L05CurQual AS Decimal(11,6)
--Set Maximum Line Speeds Speeds
SET @L05SpeedMAX = 147
SET @E05SpeedMAX = 147
SET @H05SpeedMAX = 147
SET @P05SpeedMAX = 147
SET @C05SpeedMAX = 147
SET @L05CurWO = (SELECT TOP 1 L05WO FROM WB.dbo.Line05 WHERE L05WO IS NOT NULL ORDER BY L05Time DESC)
SET @L05CurCount = (SELECT SUM(dbo.TOD(E05Count)) FROM WB.dbo.Line05 WHERE L05WO = @L05CurWO)
SET @L05CurQual = (SELECT TOP 1 CASE WHEN dbo.TOD(L05Count)<1 THEN 0 ELSE CASE WHEN dbo.TOD(L05Blowoff)<1 THEN 1 ELSE (1-(dbo.TOD(L05Blowoff)/
dbo.TOD(L05Count))) END END FROM WB.dbo.Line05 WHERE L05WO = @L05CurWO ORDER BY L05Time DESC)
--Select Current Numbers
SELECT TOP 1 @L05CurWO AS CurrentL05WorkOrder
,L05TE AS CurrentL05TE
,L05TF AS CurrentL05TF
,@L05CurCount AS CurrentL05Count
,@L05JobScrap AS CurrentL05Scrap
,L05QUAN AS CurrentL05Quantity
,dbo.TOD(L05UP)*100 AS CurrentL05Uptime
,dbo.TOD(C05SPD) AS CurrentL05Speed
,@L05CurQual*100 AS CurrentL05Qual
,CASE WHEN dbo.ToD(L05UP)>0 AND dbo.ToD(C05SPD)>0 AND @L05CurQUAL>0 THEN dbo.ToD(L05UP)*(dbo.ToD(C05SPD)/@L05SpeedMAX)*@L05CurQual ELSE 0 END AS CurrentL05OEE
,dbo.ToD(E05SPD) AS CurrentE05Speed
,dbo.ToD(E05UP)*100 AS CurrentE05Up
,dbo.ToD(E05QUAL)*100 AS CurrentE05Qual
,CASE WHEN dbo.ToD(E05UP)>0 AND dbo.ToD(E05SPD)>0 AND dbo.ToD(E05QUAL)>0 THEN (dbo.ToD(E05UP)*(dbo.ToD(E05SPD)/@E05SpeedMAX)*(1-(dbo.ToD(E05Blowoff)/
dbo.ToD(E05Count)))*100) ELSE 0 END AS CurrentE05OEE
,dbo.ToD(H05SPD) AS CurrentH05Speed
,dbo.ToD(H05UP)*100 AS CurrentH05Up
,dbo.ToD(H05QUAL)*100 AS CurrentH05Qual
,CASE WHEN dbo.ToD(H05UP)>0 AND dbo.ToD(H05SPD)>0 AND dbo.ToD(H05QUAL)>0 THEN (dbo.ToD(H05UP)*(dbo.ToD(H05SPD)/@H05SpeedMAX)*(1-(dbo.ToD(H05Blowoff)/
dbo.ToD(H05Count)))*100) ELSE 0 END AS CurrentH05OEE
,dbo.ToD(P05SPD) AS CurrentP05Speed
,dbo.ToD(P05UP)*100 AS CurrentP05Up
,dbo.ToD(P05QUAL)*100 AS CurrentP05Qual
,CASE WHEN dbo.ToD(P05UP)>0 AND dbo.ToD(P05SPD)>0 AND dbo.ToD(P05QUAL)>0 THEN (dbo.ToD(P05UP)*(dbo.ToD(P05SPD)/@P05SpeedMAX)*(1-(dbo.ToD(P05Blowoff)/
dbo.ToD(P05Count)))*100) ELSE 0 END AS CurrentP05OEE
,dbo.ToD(C05SPD) AS CurrentC05Speed
,dbo.ToD(C05UP)*100 AS CurrentC05Up
,dbo.ToD(C05QUAL)*100 AS CurrentE05Qual
,CASE WHEN dbo.ToD(C05UP)>0 AND dbo.ToD(C05SPD)>0 AND dbo.ToD(C05QUAL)>0 THEN (dbo.ToD(C05UP)*(dbo.ToD(C05SPD)/@P05SpeedMAX)*(1-(dbo.ToD(C05Blowoff)/
dbo.ToD(C05Count)))*100) ELSE 0 END AS CurrentC05OEE
FROM WB.dbo.Line05
WHERE L05WO = @L05CurWO
ORDER BY L05Time DESC