Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 a while but being unsuccessful so far.

SQL
SELECT OMCOMP,OMCUST, AccountFrom =
CASE
when OMCOMP = 'SG1'; and OMDIM1 = 1000 or OMCUST = IN(SELECT Account FROM Finance_APAC.dbo.testAccount WHERE Country = SG1;) THEN '1'
when OMCOMP = 'SG1'; and OMDIM1 = 1234 or OMCUST = IN(SELECT Account FROM Finance_APAC.dbo.testAccount WHERE Country = SG1;) THEN '2'
ELSE 'OTHERS'
END
FROM Finance_APAC.dbo.testFMOSAL



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 exists as an account 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.
Posted
Updated 3-Jan-11 23:05pm
v3
Comments
JF2015 4-Jan-11 4:11am    
Edited to add code formatting
Dalek Dave 4-Jan-11 5:05am    
Edited for Grammar and Readability.

SQL
SELECT OMCOMP,OMCUST, AccountFrom =
CASE
when OMCOMP = 'SG1' and OMDIM1 = 1000 or OMCUST IN(SELECT Account FROM Finance_APAC.dbo.testAccount WHERE Country = SG1) THEN '1'
when OMCOMP = 'SG1' and OMDIM1 = 1234 or OMCUST IN(SELECT Account FROM Finance_APAC.dbo.testAccount WHERE Country = SG1) THEN '2'
ELSE 'OTHERS'
END
FROM Finance_APAC.dbo.testFMOSAL


Try this. I have just removed '=' from ..OMCUST = IN ( SELEC... and some unnecessary semicolons.

Let me know further if it results in any error.
 
Share this answer
 
Comments
Dalek Dave 4-Jan-11 5:05am    
Excellent Answer.
Try

SQL
SELECT OMCOMP,OMCUST, AccountFrom =CASE
when (OMCOMP = 'SG1' and OMDIM1 = 1000) or (OMCOMP = 'SG1' and OMCUST = IN(SELECT Account FROM Finance_APAC.dbo.testAccount WHERE Country = 'SG1')) THEN '1'
when (OMCOMP = 'SG1' and OMDIM1 = 1234) or (OMCOMP = 'SG1' or OMCUST = IN(SELECT Account FROM Finance_APAC.dbo.testAccount WHERE Country = 'SG1')) THEN '2'
ELSE 'OTHERS'
END
FROM Finance_APAC.dbo.testFMOSAL
 
Share this answer
 
v3
Comments
Dalek Dave 4-Jan-11 5:06am    
Hiren beat you to it by a minute or so, but still a good answer, have a +5.
Abhinav S 4-Jan-11 5:08am    
Thanks DD.
SQL
SELECT OMCOMP,OMCUST,
CASE
when OMCOMP = 'SG1' and (OMDIM1 = 1000 or EXISTS(SELECT Account FROM Finance_APAC.dbo.testAccount WHERE Country = 'SG1' AND Account = OMCUST)) THEN '1'
when OMCOMP = 'SG1' and (OMDIM1 = 1234 or EXISTS(SELECT Account FROM Finance_APAC.dbo.testAccount WHERE Country = 'SG1' AND Account = OMCUST)) THEN '2'
ELSE 'OTHERS'
END AS AccountFrom 
FROM Finance_APAC.dbo.testFMOSAL



The above code should do the deed. Check data types.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900