Solved

How to use if exists in a select statement

Posted on 2008-10-02
4
598 Views
Last Modified: 2010-04-21
I need to figure out a way to indicate when a user has a monitor, a box or both. Here's the query I'm currently using:
select u.user_id,
(SELECT top 1 lm.link_monitor_id
FROM monitor_links lm
WHERE lm.user_id = u.user_id) as monitor,
(SELECT top 1 lb.link_box_id
FROM box_links lb
WHERE lb.user_id = u.user_id) as box

With this query, I get the following results:
user_id monitor_link_id box_link_id
12345   M123                B123
98765   M234                NULL
87654   NULL                B678

I would lke to get the following:
12345   both
98765   monitor
87654   box

I was thinking of using IF EXISTS, but couldn't make it work. Any ideas? Thanks!
0
Comment
Question by:horalia
4 Comments
 
LVL 18

Expert Comment

by:David Robitaille
ID: 22629734

use a case
select u.user_id,
case (SELECT 1 FROM monitor_links lm
WHERE lm.user_id = u.user_id) +
(SELECT top 1 lb.link_box_id
when 3 then 'both'
when 2 then 'monitor'
when 1 then 'box'
else 0 then 'nothing' end   as x
FROM box_links lb
WHERE lb.user_id = u.user_id)

Open in new window

0
 
LVL 35

Expert Comment

by:Terry Woods
ID: 22629760
I think this should do it:
select distinct u.user_id, "both" equipment
from user_table u inner join monitor_links lm on lm.user_id = u.user_id
                  inner join box_links lb on lb.user_id = u.user_id
union 
select distinct u.user_id, "monitor"
from user_table u inner join monitor_links lm on lm.user_id = u.user_id
                  left join box_links lb on lb.user_id = u.user_id
where lb.user_id is null
union
select distinct u.user_id, "box"
from user_table u left join monitor_links lm on lm.user_id = u.user_id
                  inner join box_links lb on lb.user_id = u.user_id
where lm.user_id is null

Open in new window

0
 
LVL 11

Accepted Solution

by:
yuching earned 500 total points
ID: 22630215
try this

SELECT u.user_id,
   (CASE WHEN b.monitor IS NOT NULL AND c.box IS NOT NULL THEN 'both'
               WHEN b.monitor IS NOT NULL THEN 'monitor'
               WHEN c.box IS NOT NULL THEN 'box'
                ELSE 'no match'
     END) As columnB
FROM usertables u
LEFT OUTER JOIN  (
     SELECT lm.user_id, MAX(lm.link_monitor_id) As monitor
     FROM monitor_links lm
      GROUP BY lm.user_id
) b ON b.user_id = u.user_id
LEFT OUTER JOIN (
      SELECT lb.user_id,  lb.link_box_id AS box
      FROM box_links lb
      GROUP BY lb.user_id
) c ON c.user_id = u.user_id
0
 

Author Closing Comment

by:horalia
ID: 31502600
Thanks!
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MS SQL - Rotating Values in SQL 9 65
Access 2010 Query Syntax 5 30
Addition to SQL for dynamic fields 6 37
SELECT query on two levels (detail and summary) 13 47
If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
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…

813 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

19 Experts available now in Live!

Get 1:1 Help Now