Quantcast
Channel: SQL Server Integration Services forum
Viewing all articles
Browse latest Browse all 24688

Using While loop need to satisfy case codition

$
0
0

I have a sample data

Dept Table :

Name
Maths
English
Social

Student Table :

Name    Subject
MOhan   English
MOhan   Maths
Rohan   English
Sajan   Social

Using while loop I have auto incremented the Dept table and using that I will count the subjects for students and mark it as 'Y' or 'N'.

Output :

Name  Maths  English  Social
Mohan  Y     Y        N
Rohan  N     Y        N
Sajan  N     N        Y

Script :

Declare@RN INT,@cnt INT,@sub varchar(20),@dept_sub VARCHAR(20),@Maths_sub BIT,@Eng_sub BIT,@soc_sub BITSET@RN = RANK()OVER(ORDERBY Name )from DeptSET@cnt = count(*)from  DeptWHILE@RN <@cnt BEGINSelect@Sub = Name from dept where RN =@RNInsertinto#Temp(Name,Maths,English,Social)Select Name,@Maths_sub =CASEWHEN count(*)>=1THEN'Y'ELSE'N'END Maths,@Eng_sub =CASEWHEN count(*)>=1THEN'Y'ELSE'N'END English,@soc_sub =CASEWHEN count(*)>=1THEN'Y'ELSE'N'END Social from student where Subject =@Sub@RN =@RN +1END

I'm not getting proper output suggest me. We can do this in PIVOT and Joins also but using loop I want to work it out


Viewing all articles
Browse latest Browse all 24688

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>