SQL Case Syntax Issue

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Yu Ong
    New Member
    • Dec 2010
    • 3

    SQL Case Syntax Issue

    Hi

    I need help in writing a case script in SQL but I am a beginner so I am doing a lot of cutting an pasteing from existing scripts and changing the variables

    I have been trying to get this to work for awhile but being unsuccessful so far.

    SELECT OMCOMP, AccountFrom =
    CASE
    when OMCOMP = 'SG1' and OMDIM1 = 1000 or OMCUST = (SELECT Account FROM Finance.dbo.tes tAccount WHERE Country = 'SG1' THEN '1')
    when OMCOMP = 'SG1' and OMDIM1 = 1234 or OMCUST = (SELECT Account FROM Finance.dbo.tes tAccount WHERE Country = 'SG1' THEN '2')
    ELSE 'OTHERS'
    END
    FROM Finance.dbo.tes tFMOSAL

    Basically I want to create a table that would show 'AccountFrom' either in '1' or '2' or ' Others'

    In the first instance, if OMCOMP is 'SG1' and OMDIM1 is 1000 or if OMCOMP is 'SG1' and OMCUST is equal to a number in another table where country is SG1 then Accountfrom will be equal to 1

    Thanks very much in advance for any expertise anyone can offer on my issue.
  • code green
    Recognized Expert Top Contributor
    • Mar 2007
    • 1726

    #2
    I don't think
    In the first instance, if OMCOMP is 'SG1' and OMDIM1 is 1000 or if OMCOMP is 'SG1' and OMCUST is equal to a number in another
    Is the same as this
    Code:
    when OMCOMP = 'SG1' and OMDIM1 = 1000 or OMCUST (SELECT ...
    Think about the logic!
    I have not looked at the second statement but the first will return true if OMCOMP is 'SG1' and OMDIM1 is 1000
    and return true if OMCUST is equal to a number in another.
    No other condition will return true

    Comment

    Working...