Home Forums SQL Server 2008 T-SQL (SS2K8) how to write a column expression in a query based on other table value exist RE: how to write a column expression in a query based on other table value exist

  • Sounds like you need a couple of left joins and a case - this should do...

    SELECT

    s.sno

    ,CASEWHEN 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;