Solved

Tricky SELECT statement

Posted on 2007-11-17
3
198 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
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

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

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

This article will describe one method to parse a delimited string into a table of data.   Why would I do that you ask?  Let's say that you need to pass multiple parameters into a stored procedure to search for.  For our sake, we'll say that we wa…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

920 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now