If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > Microsoft SQL Server > Selecting IF?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-05-12, 15:31
acidburn85 acidburn85 is offline
Registered User
 
Join Date: Sep 2011
Posts: 20
Selecting IF?

Ok so I have this script. I need it to behave like this, if the S.Term='3' then grab the data from S.WDIS otherwise leave it alone and pull whatever is in the term column. Basically that data in s.term can't be changed but I need to be able to have multiple values if it is a 3 (which denotes it as a year long class) so I have another column with the correct term code in it but the other values are all correct which are 1 and 2.

Code:
SELECT DISTINCT
	S.Course_ID, T.HR_Code, S.Section_Number, S.Room_Number, S.Period_Start, S.Term
   FROM Master_Schedule S
   LEFT JOIN Teacher T
   ON S.School_Year = T.School_Year AND S.Location = T.Teacher_School_Num AND
	S.Teacher_SSN = T.Teacher_Local_ID
		
   WHERE S.School_Year=1112 AND
	S.Location=0021 AND
	Teacher_Room<>'SFCC' ---Removes College Teachers
	ORDER BY S.Course_ID, S.Section_Number
Reply With Quote
  #2 (permalink)  
Old 01-05-12, 16:26
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,609
Code:
SELECT DISTINCT
   S.Course_ID, T.HR_Code, S.Section_Number
,  S.Room_Number, S.Period_Start, S.Term
--  vvvvv  ptp  20120105  Demo how to pick between multiple expressions for a column value
,  CASE
      WHEN '3' = S.Term THEN S.WDIS
      ELSE S.Term
   END AS CaseDemo
--  ^^^^^  ptp  20120105  Demo how to pick between multiple expressions for a column value
   FROM Master_Schedule S
      LEFT JOIN Teacher T
         ON S.School_Year = T.School_Year 
         AND S.Location = T.Teacher_School_Num 
         AND S.Teacher_SSN = T.Teacher_Local_ID
   WHERE S.School_Year = 1112
      AND S.Location=0021
      AND Teacher_Room<>'SFCC' ---Removes College Teachers
   ORDER BY S.Course_ID, S.Section_Number
-PatP
__________________
In theory, theory and practice are identical. In practice, theory and practice are unrelated.
Reply With Quote
  #3 (permalink)  
Old 01-05-12, 19:50
acidburn85 acidburn85 is offline
Registered User
 
Join Date: Sep 2011
Posts: 20
Thanks! That looks great! I owe you one. I had tried something similar buy my syntax was wrong! Thanks again!
Reply With Quote
  #4 (permalink)  
Old 01-06-12, 07:38
acidburn85 acidburn85 is offline
Registered User
 
Join Date: Sep 2011
Posts: 20
Ok I just tried it, but I was hoping to have it put data into the blanks rather than make a new column. Because the system this is importing only looks in one column. Is there any way to substitute the three with whatever is in S.WDIS?
Reply With Quote
  #5 (permalink)  
Old 01-06-12, 08:07
acidburn85 acidburn85 is offline
Registered User
 
Join Date: Sep 2011
Posts: 20
Never mind I figured it out!

Thanks for your help!!

Code:
SELECT DISTINCT
  S.Course_ID, T.HR_Code, S.Section_Number
,  S.Room_Number, S.Period_Start
--  vvvvv  ptp  20120105  Demo how to pick between multiple expressions for a column value
,  CASE
     WHEN '3' = S.Term THEN S.WDIS_Schl_Use
     WHEN '1' = S.Term THEN S.Term
     WHEN '2' = S.Term THEN S.Term
     ELSE S.Term
  END AS Term
--  ^^^^^  ptp  20120105  Demo how to pick between multiple expressions for a column value
  FROM Master_Schedule S
     LEFT JOIN Teacher T
        ON S.School_Year = T.School_Year
        AND S.Location = T.Teacher_School_Num
        AND S.Teacher_SSN = T.Teacher_Local_ID
  WHERE S.School_Year = 1112
     AND S.Location=0021
     AND Teacher_Room<>'SFCC' ---Removes College Teachers
  ORDER BY S.Course_ID, S.Section_Number, Term
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On