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


How to display Columns based on other table values


How to display Columns based on other table values

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

Group: General Forum Members
Points: 265 Visits: 328
i have below tables
1) student (sno,name,subject,cost,city),
2) studentPT (sno,Ptname,EnrollDate)
3) StudentDrill (Sno, DrillName,EnrollDate)

now the requirement is want to display sno,name,cost,PTCol (if record exist(based on sno) in studentPt table then 'Yes' + EnrollDate otherwise 'Not Enrolled'), DrillCol (if record exist(based on sno) in studentDrill table then 'Yes' + EnrollDate otherwise 'Not Enrolled')

how can i display those last two columsn based on rules

please kindly help me

Thank you Very Much in Advance
Asittii
Lynn Pettis
Lynn Pettis
SSC-Dedicated
SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)

Group: General Forum Members
Points: 39864 Visits: 38563
Look at using LEFT OUTER JOINS and either ISNULL (SQL Server specific) or COALESCE (ANSI Standard).

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
asita
asita
SSC Veteran
SSC Veteran (265 reputation)SSC Veteran (265 reputation)SSC Veteran (265 reputation)SSC Veteran (265 reputation)SSC Veteran (265 reputation)SSC Veteran (265 reputation)SSC Veteran (265 reputation)SSC Veteran (265 reputation)

Group: General Forum Members
Points: 265 Visits: 328
hi lynn,

i tried this way, am i doing correct , please correct me, is the case statement logic is good? or do i need to check some otherway

please help me, it has to be run in optimized way,, so please suggest me i fi am doing anything wrong

select
sno,
name,
enroll,
(Case when noMail.enroldate IS NULL then 'N' else 'Y' + CAST( noMail.enrollDate as varchar(20) ) end) as PTCol,
(Case when Mail.enroldate IS NULL then 'N' else 'Y' + CAST( Mail.enrollDate as varchar(20) ) end) as DrillCol,

from dbo.student t left join dbo.studentPT noMail on t.sno = noMail.sno
left join dbo.studentDrill Mail on t.sno = Mail.sno
Arjun Sivadasan
Arjun Sivadasan
Say Hey Kid
Say Hey Kid (672 reputation)Say Hey Kid (672 reputation)Say Hey Kid (672 reputation)Say Hey Kid (672 reputation)Say Hey Kid (672 reputation)Say Hey Kid (672 reputation)Say Hey Kid (672 reputation)Say Hey Kid (672 reputation)

Group: General Forum Members
Points: 672 Visits: 976
Cool, that's correct. Also, make it a practice to check out execution plans of all queries that you write, if you don't do that already (Ctrl + M).
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