6

我正在尝试在 where 子句中执行某种“if”语句。我意识到 sql 不支持这一点,但我确信必须有某种方法可以使其与 sql 语法一起使用。如我以粗体显示的区域所示,我试图找到所有以 d 开头的项目,如果它们的 userfld2 也 = container.

有没有比我现在做的更合理的方法,还是我离题了?

提前致谢。

Select a.ItemID
   , b.ConversionFactor VCaseAmt
   , sum(c.ConversionFactor + 1) SCaseAmt
   , a.status
   , a.UserFld2
From timItem a
inner join timItemUnitOfMeas b on a.ItemKey = b.ItemKey
   and b.TargetUnitMeasKey = 115
left join timItemUnitOfMeas c on a.ItemKey = c.ItemKey
   and c.TargetUnitMeasKey = 116
left join timItemUnitOfMeas d on a.ItemKey = d.ItemKey
   and d.TargetUnitMeasKey = 126
Where d.TargetUnitMeasKey is null
   and b.ConversionFactor != c.ConversionFactor + 1
   and a.Status = 1
   and **(filter a.itemid not like 'd%' when a.userfld2 = 'Container')**
Group by a.ItemID, b.TargetUnitMeasKey, b.ConversionFactor, C.TargetUnitMeasKey
   , c.ConversionFactor, a.status, a.UserFld2
Order by a.ItemID
4

1 回答 1

1

用这个:

Select a.ItemID, b.ConversionFactor VCaseAmt, sum(c.ConversionFactor + 1) SCaseAmt, a.status, a.UserFld2

From timItem a inner join
    timItemUnitOfMeas b on a.ItemKey = b.ItemKey and b.TargetUnitMeasKey = 115 left join
    timItemUnitOfMeas c on a.ItemKey = c.ItemKey and c.TargetUnitMeasKey = 116 left join
    timItemUnitOfMeas d on a.ItemKey = d.ItemKey and d.TargetUnitMeasKey = 126

Where d.TargetUnitMeasKey is null and b.ConversionFactor != c.ConversionFactor + 1 and a.Status = 1 and 
not (a.itemid like 'd%' AND a.userfld2 = 'Container') 

Group by a.ItemID, b.TargetUnitMeasKey, b.ConversionFactor, C.TargetUnitMeasKey, c.ConversionFactor, a.status, a.UserFld2

Order by a.ItemID
于 2013-04-16T18:15:22.460 回答