Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

How to display Columns based on other table values Expand / Collapse
Author
Message
Posted Wednesday, February 27, 2013 12:28 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Friday, August 15, 2014 5:48 AM
Points: 140, Visits: 281


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
Post #1424687
Posted Wednesday, February 27, 2013 12:37 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Yesterday @ 6:19 PM
Points: 23,243, Visits: 31,938
Look at using LEFT OUTER JOINS and either ISNULL (SQL Server specific) or COALESCE (ANSI Standard).



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)
Post #1424693
Posted Wednesday, February 27, 2013 12:54 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Friday, August 15, 2014 5:48 AM
Points: 140, Visits: 281
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
Post #1424712
Posted Wednesday, February 27, 2013 9:02 PM


SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Monday, August 11, 2014 3:57 AM
Points: 406, Visits: 773
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).
Post #1424873
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse