Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 IF statement inside a select statement???

Author  Topic 

Vack
Aged Yak Warrior

530 Posts

Posted - 2010-01-20 : 13:56:00
In the code below, it is possible to add the condition

old_quantity - quantity if doc_type = 'I'

All other doc_types would be old_quantity + quantity


SELECT *, old_quantity + quantity AS NewQTy
FROM dbo.[old qty] s
WHERE (trx_tm =
(SELECT MAX(trx_tm)
FROM [old qty]
WHERE item_no = S.item_no))
GROUP BY item_no, old_quantity, trx_dt, trx_tm, loc, doc_type, quantity

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-01-20 : 13:59:31
An approach:

CASE doc_type
WHEN 'I' THEN old_quantity - quantity
ELSE old_quantity + quantity
END as NewQty



No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

Vack
Aged Yak Warrior

530 Posts

Posted - 2010-01-20 : 14:00:08
Just read my post and may need to clarify.

I want the condition on my field NewQty

If Doc_type = 'I' then I want to subtract.
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-01-20 : 14:01:26
So what is wrong with my approach?


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

Vack
Aged Yak Warrior

530 Posts

Posted - 2010-01-20 : 15:07:17
Sorry, I put that in right after you replied. It worked fine.

Thanks
Go to Top of Page
   

- Advertisement -