乘以 T-SQL 连接查询中的列
Multiply columns in T-SQL join query
我正在使用 SQL Server 2008。我正在尝试构建一个 T-SQL 查询,以根据来自多个 table 的数据计算一些性能指标。不幸的是,我被困在其中一个计算上,无法弄清楚哪里出了问题。如果有任何帮助,我将不胜感激:
计算需要总生产量(来自table ShiftHourCounts
),总报废件数(来自table ShiftReportScrap
),以及总停机时间(来自 Downtime
查询)
我尝试在查询中添加每个 operation/calculation 主要是为了我自己的教育/故障排除
我不明白为什么 Q return 列为零。 Q= Tok/Tp,查询正确 returns 并分别计算 Tok 和 Tp。在下面的例子中,Q 应该 =0.994
查询目前 return 对除 Q、A、P 和 OEE 之外的所有内容的正确值。 Q、A、P 和 OEE 总是 return 零
当前查询:
--OEE= A*P*Q (this is the final desired result/ calculation)
--A= (Planned run time - Unplanned Down Time)/Planned run time
--A= (Prt - Dtu)/Prt
--Prt= Maximum Available Time - Planned Down Time
--Prt= Mat=DTp
--Effective production time= Planned run time - Unplanned Down Time
--Ept=Prt-DTu
--P= (BDT*total number of produced parts)/Effective production time
--P= (BDT*Tp)/Ept
--Q= Total number of OK parts/Total number of produced parts
--Q= Tok/Tp
select
sm.SR_ID, sm.SR_PartID, sm.SR_StartTime,
isnull(sm.SR_EndTime,GETDATE()) AS EndTime,
isnull(sm.SR_BDT,1) AS BDT,
DATEDIFF(n, sm.SR_StartTime, isnull(sm.SR_EndTime, GETDATE())) AS Prt,
isnull(p.TotalProduced,0) AS Tp,
isnull(s.Scrap,0) AS Scrap,
(isnull(p.TotalProduced, 0) - isnull(s.Scrap, 0)) AS Tok,
isnull(dt.DownTimeDuration, 0) AS DTu,
((isnull(p.TotalProduced, 0) - isnull(s.Scrap, 0)) / isnull(p.TotalProduced, 0)) AS Q, --Q= Tok/Tp
((DATEDIFF(n, sm.SR_StartTime, isnull(sm.SR_EndTime, GETDATE())) - isnull(dt.DownTimeDuration, 0)) / DATEDIFF(n, sm.SR_StartTime, isnull(sm.SR_EndTime, GETDATE()))) AS A,
((isnull(sm.SR_BDT, 1) * isnull(p.TotalProduced, 0)) / (DATEDIFF(n, sm.SR_StartTime, isnull(sm.SR_EndTime, GETDATE())) - isnull(dt.DownTimeDuration, 0))) AS P,
(((isnull(p.TotalProduced, 0) - isnull(s.Scrap, 0)) / isnull(p.TotalProduced, 0)) * ((DATEDIFF(n, sm.SR_StartTime, isnull(sm.SR_EndTime, GETDATE())) - isnull(dt.DownTimeDuration, 0)) / DATEDIFF(n, sm.SR_StartTime, isnull(sm.SR_EndTime, GETDATE())))*((isnull(sm.SR_BDT,1)*isnull(p.TotalProduced,0))/(DATEDIFF(n,sm.SR_StartTime,isnull(sm.SR_EndTime,GETDATE()))-isnull(dt.DownTimeDuration,0)))) AS OEE
FROM
ShiftReportMaster sm
LEFT JOIN
(SELECT
SH_ShiftID, Sum(SH_Produced) AS TotalProduced
FROM
ShiftHourCounts
GROUP BY
SH_ShiftID) p ON (p.SH_ShiftID = sm.SR_ID)
LEFT JOIN
(SELECT
SRS_SR_ID, SRS_PartID, Sum(SRS_Scraped) AS Scrap
FROM
ShiftReportScrap
GROUP BY
SRS_SR_ID, SRS_PartID) s ON (s.SRS_SR_ID = sm.SR_ID)
AND (s.SRS_PartID = sm.SR_PartID)
LEFT JOIN
(SELECT
srd.DTR_SRID, [Downtime reasons].DT_Planned,
Sum(srd.DTR_DownTimeDuration) AS DownTimeDuration
FROM
ShiftReportDowntime srd
LEFT JOIN
[Downtime reasons] ON srd.DTR_Reason = [Downtime reasons].DT_ID
GROUP BY
srd.DTR_SRID, [Downtime reasons].DT_Planned
HAVING
((([Downtime reasons].DT_Planned) = 0))) dt ON (dt.DTR_SRID = sm.SR_ID)
WHERE
sm.SR_ID = 3689;
很可能来自整数除法。试试这个:
((isnull(p.TotalProduced+.0,0.0)-isnull(s.Scrap+.0,0.0))
/nullif(p.TotalProduced,0)) AS Q, --Q= Tok/Tp
加法 .0
或乘法 1.0
隐含地将整数转换为 decimal
类型。
除以整数将 return 一个整数类型,如果该值小于 1
它将 return 0
因为它截断而不是四舍五入或使用一些其他逻辑 return 一个整数。
我正在使用 SQL Server 2008。我正在尝试构建一个 T-SQL 查询,以根据来自多个 table 的数据计算一些性能指标。不幸的是,我被困在其中一个计算上,无法弄清楚哪里出了问题。如果有任何帮助,我将不胜感激:
计算需要总生产量(来自table
ShiftHourCounts
),总报废件数(来自tableShiftReportScrap
),以及总停机时间(来自Downtime
查询)我尝试在查询中添加每个 operation/calculation 主要是为了我自己的教育/故障排除
我不明白为什么 Q return 列为零。 Q= Tok/Tp,查询正确 returns 并分别计算 Tok 和 Tp。在下面的例子中,Q 应该 =0.994
查询目前 return 对除 Q、A、P 和 OEE 之外的所有内容的正确值。 Q、A、P 和 OEE 总是 return 零
当前查询:
--OEE= A*P*Q (this is the final desired result/ calculation)
--A= (Planned run time - Unplanned Down Time)/Planned run time
--A= (Prt - Dtu)/Prt
--Prt= Maximum Available Time - Planned Down Time
--Prt= Mat=DTp
--Effective production time= Planned run time - Unplanned Down Time
--Ept=Prt-DTu
--P= (BDT*total number of produced parts)/Effective production time
--P= (BDT*Tp)/Ept
--Q= Total number of OK parts/Total number of produced parts
--Q= Tok/Tp
select
sm.SR_ID, sm.SR_PartID, sm.SR_StartTime,
isnull(sm.SR_EndTime,GETDATE()) AS EndTime,
isnull(sm.SR_BDT,1) AS BDT,
DATEDIFF(n, sm.SR_StartTime, isnull(sm.SR_EndTime, GETDATE())) AS Prt,
isnull(p.TotalProduced,0) AS Tp,
isnull(s.Scrap,0) AS Scrap,
(isnull(p.TotalProduced, 0) - isnull(s.Scrap, 0)) AS Tok,
isnull(dt.DownTimeDuration, 0) AS DTu,
((isnull(p.TotalProduced, 0) - isnull(s.Scrap, 0)) / isnull(p.TotalProduced, 0)) AS Q, --Q= Tok/Tp
((DATEDIFF(n, sm.SR_StartTime, isnull(sm.SR_EndTime, GETDATE())) - isnull(dt.DownTimeDuration, 0)) / DATEDIFF(n, sm.SR_StartTime, isnull(sm.SR_EndTime, GETDATE()))) AS A,
((isnull(sm.SR_BDT, 1) * isnull(p.TotalProduced, 0)) / (DATEDIFF(n, sm.SR_StartTime, isnull(sm.SR_EndTime, GETDATE())) - isnull(dt.DownTimeDuration, 0))) AS P,
(((isnull(p.TotalProduced, 0) - isnull(s.Scrap, 0)) / isnull(p.TotalProduced, 0)) * ((DATEDIFF(n, sm.SR_StartTime, isnull(sm.SR_EndTime, GETDATE())) - isnull(dt.DownTimeDuration, 0)) / DATEDIFF(n, sm.SR_StartTime, isnull(sm.SR_EndTime, GETDATE())))*((isnull(sm.SR_BDT,1)*isnull(p.TotalProduced,0))/(DATEDIFF(n,sm.SR_StartTime,isnull(sm.SR_EndTime,GETDATE()))-isnull(dt.DownTimeDuration,0)))) AS OEE
FROM
ShiftReportMaster sm
LEFT JOIN
(SELECT
SH_ShiftID, Sum(SH_Produced) AS TotalProduced
FROM
ShiftHourCounts
GROUP BY
SH_ShiftID) p ON (p.SH_ShiftID = sm.SR_ID)
LEFT JOIN
(SELECT
SRS_SR_ID, SRS_PartID, Sum(SRS_Scraped) AS Scrap
FROM
ShiftReportScrap
GROUP BY
SRS_SR_ID, SRS_PartID) s ON (s.SRS_SR_ID = sm.SR_ID)
AND (s.SRS_PartID = sm.SR_PartID)
LEFT JOIN
(SELECT
srd.DTR_SRID, [Downtime reasons].DT_Planned,
Sum(srd.DTR_DownTimeDuration) AS DownTimeDuration
FROM
ShiftReportDowntime srd
LEFT JOIN
[Downtime reasons] ON srd.DTR_Reason = [Downtime reasons].DT_ID
GROUP BY
srd.DTR_SRID, [Downtime reasons].DT_Planned
HAVING
((([Downtime reasons].DT_Planned) = 0))) dt ON (dt.DTR_SRID = sm.SR_ID)
WHERE
sm.SR_ID = 3689;
很可能来自整数除法。试试这个:
((isnull(p.TotalProduced+.0,0.0)-isnull(s.Scrap+.0,0.0))
/nullif(p.TotalProduced,0)) AS Q, --Q= Tok/Tp
加法 .0
或乘法 1.0
隐含地将整数转换为 decimal
类型。
除以整数将 return 一个整数类型,如果该值小于 1
它将 return 0
因为它截断而不是四舍五入或使用一些其他逻辑 return 一个整数。