SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


how to write a column expression in a query based on other table value exist


how to write a column expression in a query based on other table value exist

Author
Message
asita
asita
SSC Veteran
SSC Veteran (263 reputation)SSC Veteran (263 reputation)SSC Veteran (263 reputation)SSC Veteran (263 reputation)SSC Veteran (263 reputation)SSC Veteran (263 reputation)SSC Veteran (263 reputation)SSC Veteran (263 reputation)

Group: General Forum Members
Points: 263 Visits: 328
Hello Please help me here.,

i have below tables called

student (sno, sname, address)
StudentDrillEnrolled (sno, DrillClass)
StudentDrillNotEnrolled (Sno, Reason, Viever)

how to write a query, so that i will have sno,sname,address, Enrolled (Y/N/NA)

enrolled will be calculated
if sno exist in StudentDrillEnrolled table then Y
if sno exist in StudentDrillNotEnrolled table then it is N
if not exist in both tables enrolled & not enrolled then N/A

please help me here

Thanks in advance
asitti
Hotblack
Hotblack
SSC-Enthusiastic
SSC-Enthusiastic (172 reputation)SSC-Enthusiastic (172 reputation)SSC-Enthusiastic (172 reputation)SSC-Enthusiastic (172 reputation)SSC-Enthusiastic (172 reputation)SSC-Enthusiastic (172 reputation)SSC-Enthusiastic (172 reputation)SSC-Enthusiastic (172 reputation)

Group: General Forum Members
Points: 172 Visits: 113
Sounds like you need a couple of left joins and a case - this should do...

SELECT
s.sno
,CASE WHEN e.sno IS NOT NULL THEN
'Y'
WHEN n.sno IS NOT NULL THEN
'N'
WHEN e.sno IS NULL AND n.sno IS NULL THEN
'N/A'
END
FROM student s
LEFT JOIN StudentDrillEnrolled e
ON s.sno=e.sno
LEFT JOIN StudentDrillNotEnrolled n
ON s.sno=n.sno;



ScottPletcher
ScottPletcher
SSCertifiable
SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)

Group: General Forum Members
Points: 7827 Visits: 7145
Or you can use EXISTS:


SELECT
sno, sname, address,
CASE
WHEN EXISTS(SELECT 1 FROM dbo.StudentDrillEnrolled sde WHERE sde.sno = s.sno) THEN 'Y'
WHEN EXISTS(SELECT 1 FROM dbo.StudentDrillNotEnrolled sdne WHERE sdne.sno = s.sno) THEN 'N'
ELSE 'N/A'
END AS Enrolled
FROM dbo.student s



SQL DBA,SQL Server MVP(07, 08, 09)[size=2]Prosecutor James Blackburn, in closing argument in the Fatal Vision murders trial: If in the future, you should cry a tear, cry one for them [the murder victims]. If in the future, you should say a prayer, say one for them. And if in the future, you should light a candle, light one for them.[/size]
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search