Solved

Tricky SELECT statement

Posted on 2007-11-17
3
188 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
Comment Utility

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
Comment Utility
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
Comment Utility
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

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Join & Write a Comment

There are some very powerful Data Management Views (DMV's) introduced with SQL 2005. The two in particular that we are going to discuss are sys.dm_db_index_usage_stats and sys.dm_db_index_operational_stats.   Recently, I was involved in a discu…
Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

771 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

10 Experts available now in Live!

Get 1:1 Help Now