2013-04-16 5 views
6

where句で何らかの "if"文を実行しようとしています。私はSQLがこれをサポートしていないことを認識していますが、私はSQL構文でこの作業を行うための何らかの方法が必要であると確信しています。私が太字で示した領域に示されているように、私はdで始まるすべての項目を見つけようとしており、userfld2 = containerでもそれらをフィルタリングしています。SQL where句で "if" type文を実行する

私がやっているよりももっと合理的なやり方がありますか?

ありがとうございます。

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 
+1

フィルタa.itemid 'Dの%' **と** a.userfld2 = 'コンテナ' を好きではありませんか? – NINCOMPOOP

答えて

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 
+0

ビンゴ。それはまさにそれです。数時間後に私はそれを動作させるために過ごした。しかし、ありがとう。 – Aarmora