我试图在我的 where 子句中做某种“if”语句。我意识到 sql 不支持这一点,但我确信必须有某种方法可以使这个与 sql 语法一起工作。如我以粗体显示的区域所示,我试图找到所有以 d 开头的项目,如果它们的 userfld2 也 = 容器,则将它们过滤掉。
有没有比我正在做的更合理的方法来做到这一点,或者我离题了?
提前致谢。
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
最佳答案
用这个:
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
关于sql - 在 sql where 子句中执行 "if"类型语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16044112/