Solved

Tricky SELECT statement

Posted on 2007-11-17
3
222 Views
Last Modified: 2010-04-21
I need to create  a SELECT statement against two tables:

Table 1: Languages
Language_ID
Language_Descr

Table 2: Narrators_Languages
Narrator_ID
Language_ID

I need to return all records in the Languages table, and specify a Narrator_ID from the Narrators_Languages table. For each record, return 'YES' if there is a record for the specified Narrator_ID else return 'NO'.

Example:
Languages 'EN', 'FR', 'SP'
Narrators_Languages: Narrator_ID = 2 and Language_ID = 'EN', Narrator_ID = 2 and Language_ID = 'SP'
(no record for Narrator_ID = 2 and Language_ID = 'FR')

Desired result:
Narrator_ID  Language_ID  Match
    2                'EN'                YES
    2                'SP'                YES
    2                'FR'                 NO

Thanks for any help with this!
0
Comment
Question by:wlevy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 28

Accepted Solution

by:
imran_fast earned 250 total points
ID: 20306798

select A.Narrator_ID,A.Language_ID,case when N.Language_ID is null then 'No' else 'Yes' end Match
FROM
(select Distinct N.Narrator_ID,  L.Language_ID
from Narrators_Languages N
cross join
Languages L)  A
left outer join Narrators_Languages N
on A.Narrator_ID = N.Narrator_ID
and N.Language_ID = A.Language_ID

0
 
LVL 25

Assisted Solution

by:imitchie
imitchie earned 250 total points
ID: 20307126
declare @narrator_id int set @narrator_id = 2   --- <-- specify a Narrator_ID

select @narrator_id, l.language_id,
 case when n.narrator_id is null then 'NO' else 'YES' end as Match
from languages l
 left join narrators_languages n
  on l.language_id = n.language_id and n.narrator_id = @narrator_id
order by 3 desc, 2 asc
0
 

Author Closing Comment

by:wlevy
ID: 31409776
Both excellent - thank you very much! I was close to getting this on my own but not quite there... this is perfect.
0

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

So every once in a while at work I am asked to export data from one table and insert it into another on a different server.  I hate doing this.  There's so many different tables and data types.  Some column data needs quoted and some doesn't.  What …
INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question