2013-04-16 14 views
6

Próbuję wykonać instrukcję "if" w mojej klauzuli where. Rozumiem, że sql nie obsługuje tego, ale jestem pewien, że musi być jakiś sposób, aby to działało ze składnią sql. Jak pokazano w obszarze, który mam pogrubiony Próbuję znaleźć wszystkie elementy, które zaczynają się od d i odfiltrować je, jeśli ich userfld2 również = kontener.Wykonywanie instrukcji typu "if" w sql gdzie klauzula

Czy jest rozsądniejszy sposób na zrobienie tego niż robię, czy też nie jestem zbyt dobry?

Z góry dziękuję.

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

filtr a.itemid nie jak 'D%' ** i ** a.userfld2 = 'pojemnik'? – NINCOMPOOP

Odpowiedz

1

Służy:

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

Bingo. Dokładnie to. Frustrujące po godzinach, które spędziłem próbując sprawić, by działało. Ale bardzo dziękuję. – Aarmora