views:

40

answers:

2

I'm trying to join two tables, one is a table of tags, and another is a table that bridges tagID and slotID. I want to create a sproc that returns the list of tags, and a column of slots that use those categories.

tagDetails

tagID   tagDescription

  1      red  
  2      blue  
  3      green  
  4      purple

tagBridge

tagID  slotID

  1         1    
  1         3  
  2         5  
  3         1  
  4         1  

and i'd like the sproc to return a result like this if it takes a slotID parameter of 1

tagID   tagDescription    slotID
  1      red           yes  
  2      blue           no  
  3      green         yes  
  4      purple        yes  

i made a sproc like this but i know its not really going to work properly

SELECT tagDetails.tagID, tagDetails.tagDescription, tagBridge.slotID
FROM tagDetails
LEFT JOIN tagBridge
ON tagDetails.tagID = tagBridge.tagID
where tagBridge.slotID = 1

any ideas?

+1  A: 

easy:

  Select tagID,   tagDescription,
    Case When Exists 
      (Select * From tagBridge
       Where tagId = t.TagId
          And slotId = @SlotId)
     Then 'yes' else 'no' End SlotId
  From tagdetails t
Charles Bretana
`Case When Exists ` presumably?
Martin Smith
Great, had to modify it a little to get it to work. Select tagID, tagDescription, Case When Exists (Select * From tagBridge Where tagId = t.TagId And slotId = @SlotId) Then 'yes' else 'no' End as SlotId From tagdetails tThis works a treat for anyone looking to do the same. Many many thanks!
Tim Butler
ahhh typo... `When` not `Where` ...
Charles Bretana
+2  A: 

Use:

   SELECT td.tagid,
          td.tagdescription,
          CASE 
            WHEN tb.slotid = 1 THEN 'Yes'
            ELSE 'No'
          END AS slotid
     FROM TAGDETAILS td
LEFT JOIN TAGBRIDGE tb ON tb.tagid = td.tagid
OMG Ponies
This almost works, the yes/no column works great, however it shows multiple tagid's where as i just want it to show tagId 1 > 4 and the yes/no column, so basically distinct td.tagid...thanks again
Tim Butler
@Tim Butler: Sorry - I don't understand what you mean by "to show tagId 1 > 4 and the yes/no column" - do you only want tags 1-4?
OMG Ponies