Solved

Tricky SELECT statement

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

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…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

785 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