SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Need help with CASE statement
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Niki
Yak Posting Veteran

51 Posts

Posted - 12/20/2013 :  10:56:42  Show Profile  Reply with Quote
Hi,
I would like to draw an ANSWER from the 4 questions that are posed in a survey.
Here are the rules
- If all Q1,Q2,Q3 & Q4 = ‘Yes’ then ANSWER = ‘Yes’
- If there are only 2 questions answered and 2 left blank, pick the one that is answered other than ‘No’ out of the answered responses
- If there are three responses, then use the answer other than ‘No’ which appears in Q2, then Q3, then Q1.
I need help to build a CASE statement for the above.
Thank you,


Niki

Lamprey
Flowing Fount of Yak Knowledge

4614 Posts

Posted - 12/20/2013 :  11:37:56  Show Profile  Reply with Quote
Can you supply some sample data with expected output in a consumable format? Here are some links to help you provide that:

http://www.sqlservercentral.com/articles/Best+Practices/61537/
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

PS - We don't do homework, but we can help guide you.
Go to Top of Page

Niki
Yak Posting Veteran

51 Posts

Posted - 12/20/2013 :  15:01:31  Show Profile  Reply with Quote
Sorry, I was really under pressure to make this update. I accomplished using multiple case statements as below. I am looking for a better way to accomplish the same task.


Update Table1 set

t1.HomLang =
--if the HLS = all English, For EL students, HomLang "Unknown"
case when (lng.FirstlearntLang = '00' and lng.LangSpokenbyAdults = '00' and lng.LangSpokenbychild = '00' ) then 'UU'

--When there are only two languages listed out of Q1,Q2 &Q3, and one is English, use the language other than English

when (lng.FirstlearntLang is null and lng.LangSpokenbyAdults is not null and lng.LangSpokenbychild is not null) and lng.LangSpokenbyAdults = '00' then lng.LangSpokenbychild
when(lng.FirstlearntLang is null and lng.LangSpokenbyAdults is not null and lng.LangSpokenbychild is not null) and lng.LangSpokenbychild = '00' then lng.LangSpokenbyAdults

when (lng.LangSpokenbyAdults is null and lng.FirstlearntLang is not null and lng.LangSpokenbychild is not null) and lng.FirstlearntLang = '00' then lng.LangSpokenbychild
when (lng.LangSpokenbyAdults is null and lng.FirstlearntLang is not null and lng.LangSpokenbychild is not null) and lng.LangSpokenbychild = '00' then lng.FirstlearntLang

when (lng.LangSpokenbychild is null and lng.FirstlearntLang is not null and lng.LangSpokenbyAdults is not null) and lng.FirstlearntLang = '00' then lng.LangSpokenbyAdults
when (lng.LangSpokenbychild is null and lng.FirstlearntLang is not null and lng.LangSpokenbyAdults is not null) and lng.LangSpokenbyAdults = '00' then lng.FirstlearntLang

--If there are three languages listed, then use the language other than English which appears in Q2, then Q3, then Q1
-- possibility of one of them being English

when (lng.FirstlearntLang = '00' and lng.LangSpokenbyAdults != '00' and lng.LangSpokenbychild != '00') then lng.LangSpokenbyAdults
when (lng.LangSpokenbyAdults = '00' and lng.FirstlearntLang != '00' and lng.LangSpokenbychild != '00') then lng.LangSpokenbychild
when (lng.LangSpokenbychild = '00' and lng.FirstlearntLang != '00' and lng.LangSpokenbyAdults != '00') then lng.LangSpokenbyAdults

-- when all three are non-english L2 takes priority over L3 take priority over L1
when ( lng.FirstlearntLang != '00' and lng.LangSpokenbyAdults != '00' and lng.LangSpokenbychild != '00' ) then lng.LangSpokenbyAdults

--If there is only one language listed, and it’s not English, pick this
when ( lng.FirstlearntLang is null and lng.LangSpokenbyAdults is null and lng.LangSpokenbychild != '00' ) then lng.LangSpokenbychild
when ( lng.FirstlearntLang is null and lng.LangSpokenbyAdults != '00' and lng.LangSpokenbychild is null ) then lng.LangSpokenbyAdults
when ( lng.FirstlearntLang != '00' and lng.LangSpokenbyAdults is null and lng.LangSpokenbychild is null ) then lng.FirstlearntLang

--If there is only one non-English, pick this
when ( lng.FirstlearntLang ='00' and lng.LangSpokenbyAdults = '00' and lng.LangSpokenbychild != '00' ) then lng.LangSpokenbychild
when ( lng.FirstlearntLang = '00' and lng.LangSpokenbyAdults != '00' and lng.LangSpokenbychild = '00' ) then lng.LangSpokenbyAdults
when ( lng.FirstlearntLang != '00' and lng.LangSpokenbyAdults = '00' and lng.LangSpokenbychild = '00' ) then lng.FirstlearntLang
end
from Table1 t1
INNER JOIN table2 lng ON t1.table_GU = lng.table_GU
WHERE t1.HomLang = '00'


commit transaction
end try

begin catch
if @@trancount > 0
rollback

DECLARE @ErrMsg nvarchar(4000), @ErrSeverity int
SELECT @ErrMsg = ERROR_MESSAGE(),
@ErrSeverity = ERROR_SEVERITY()

RAISERROR(@ErrMsg, @ErrSeverity, 1)
end catch


Niki
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 12/21/2013 :  01:08:52  Show Profile  Reply with Quote
Unless you post some sample data and explain what your exact requirement is its difficult to help you out. follow the link posted and give sample data in required format.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.12 seconds. Powered By: Snitz Forums 2000