0

我有以下存储过程:

@Beginn Datetime,
@End Datetime
as
Begin
select  DATE,
IF((((SUM(ScheduleTime*(OkParts/100))/ SUM(PLannedTime)))) >1) then 1 else (((SUM(ScheduleTime*(OkParts/100))/ SUM(PLannedTime)))
from Rawdata where PartNo>0 and DATE> @Beginn and DATE< @End 
Group by DATE
Order by DATE DESC
End

但是,该过程会引发错误。

计算值是一个百分比值。由于用户输入不正确,在极少数情况下该值可能高达 250%,因此我想返回 1(逻辑上该值只能为 100%),否则只返回计算值。我还没有找到为存储过程返回的值设置绝对上限的函数。

4

2 回答 2

0

可能这对你有用:

@Beginn Datetime,
@End Datetime
as
Begin
select  DATE,
case when SUM(ScheduleTime*(OkParts/100))/ SUM(PLannedTime) >1 then 1 
    else SUM(ScheduleTime*(OkParts/100))/ SUM(PLannedTime) end
from Rawdata where PartNo>0 and DATE> @Beginn and DATE< @End 
Group by DATE
Order by DATE DESC
End
于 2013-10-11T07:01:35.920 回答
0

问题是您不能在 select 语句中使用流控制语句(例如 if...else、while...)。您可以改用 CASE 语句。

Declare @Beginn Datetime
Declare @End Datetime

as
Begin
select  DATE,
(case when SUM(ScheduleTime*(OkParts/100))/ SUM(PLannedTime) >1 then 1 
    else SUM(ScheduleTime*(OkParts/100))/ SUM(PLannedTime) end) as ALIAS
from Rawdata where PartNo>0 and DATE> @Beginn and DATE< @End
Group by DATE
Order by DATE DESC
End
于 2016-08-03T16:29:03.307 回答