MS SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsDatabasesMS SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old July 26th, 2012, 02:26 PM
Night1505 Night1505 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2012
Posts: 5 Night1505 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 52 m
Reputation Power: 0
Return columns from either table B or C based on a match in Table A

This is stumping me terrribly, so I will try to break it down as best I can.

I'm trying to return all columns from Table A, and either certain columns from TableB or certain columns from TableC, depending on which one of the latter two tables contains a match with a column in TableA.

More specifically, I have TableA which contains a column called "LocationID". TableB contains a column called "SiteID". TableC contains a column called "DealerCode".

If TableA.LocationID = TableB.SiteID, then I need to return columns from TableB, but if TableA.LocationID = TableC.DealerCode, then I need to return columns from TableC.

Here is an example of some SQL I wrote for SQL Server 2005; it is syntactically correct, but times out when I'm running it. Also, when I actually do manage to tweak it so that it will run, I get tons of duplicates from TableA. I tried using a DISTINCT, but that made it time out again. Here is an example of what I'm trying to do:

SELECT

TA.Column1, TA.Column2, TA.Column3, TA.Column4, TA.Column5, TA.Column6, TA.Column7, TA.Column9, TA.Column10, TA.Column11, TA.Column12, TA.Column13, TA.Column14, TA.Column15, TA.Column16, TA.Column17, TA.Column18, TA.LocationId, (CASE WHEN TA.LocationID IN (SELECT SiteID FROM TableB) THEN TB.SiteID WHEN TA.LocationID IN (SELECT DealerCode FROM TableC) THEN TC.DealerCode END) As SiteID, (CASE WHEN TA.LocationID IN (SELECT SiteID FROM TableB) THEN TB.Column3 WHEN TA.LocationID IN (SELECT DealerCode FROM TableC) THEN TC.Column3 END) As LocationName, (CASE WHEN TA.LocationID IN (SELECT SiteID FROM TableB) THEN TB.Column4 WHEN TA.LocationID IN (SELECT DealerCode FROM TableC) THEN TC.Column4 END) As Address1, (CASE WHEN TA.LocationID IN (SELECT SiteID FROM TableB) THEN TB.Column5 WHEN TA.LocationID IN (SELECT DealerCode FROM TableC) THEN TC.Column5 END) As City

FROM

dbo.TableA AS TA LEFT OUTER JOIN
dbo.TableB AS TB ON TB.SiteID = TA.LocationId LEFT OUTER JOIN
dbo.TableC AS TC ON TC.DealerCode = TA.LocationId

ORDER BY TA.Column2

Any help would be greatly appreciated.

Thanks in advance!

--Chris D.

Reply With Quote
  #2  
Old July 27th, 2012, 05:29 AM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 26,375 r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 1 Week 2 Days 7 h 30 m 16 sec
Reputation Power: 4140
Code:
SELECT TA.Column1
     , TA.Column2
     , TA.Column3
     , TA.Column4
     , TA.Column5
     , TA.Column6
     , TA.Column7
     , TA.Column9
     , TA.Column10
     , TA.Column11
     , TA.Column12
     , TA.Column13
     , TA.Column14
     , TA.Column15
     , TA.Column16
     , TA.Column17
     , TA.Column18
     , TA.LocationId
     , CASE WHEN TA.LocationID = TB.SiteID     THEN TB.SiteID  
            WHEN TA.LocationID = TC.DealerCode THEN TC.DealerCode END  AS SiteID
     , CASE WHEN TA.LocationID = TB.SiteID     THEN TB.Column3 
            WHEN TA.LocationID = TC.DealerCode THEN TC.Column3    END  AS LocationName
     , CASE WHEN TA.LocationID = TB.SiteID     THEN TB.Column4 
            WHEN TA.LocationID = TC.DealerCode THEN TC.Column4    END  AS Address1
     , CASE WHEN TA.LocationID = TB.SiteID     THEN TB.Column5 
            WHEN TA.LocationID = TC.DealerCode THEN TC.Column5    END  AS City
  FROM dbo.TableA AS TA 
LEFT OUTER 
  JOIN dbo.TableB AS TB 
    ON TB.SiteID = TA.LocationId 
LEFT OUTER 
  JOIN dbo.TableC AS TC 
    ON TC.DealerCode = TA.LocationId
ORDER 
    BY TA.Column2
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book

Reply With Quote
  #3  
Old July 29th, 2012, 10:49 AM
Night1505 Night1505 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2012
Posts: 5 Night1505 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 52 m
Reputation Power: 0
Quote:
Originally Posted by r937
Code:
SELECT TA.Column1
     , TA.Column2
     , TA.Column3
     , TA.Column4
     , TA.Column5
     , TA.Column6
     , TA.Column7
     , TA.Column9
     , TA.Column10
     , TA.Column11
     , TA.Column12
     , TA.Column13
     , TA.Column14
     , TA.Column15
     , TA.Column16
     , TA.Column17
     , TA.Column18
     , TA.LocationId
     , CASE WHEN TA.LocationID = TB.SiteID     THEN TB.SiteID  
            WHEN TA.LocationID = TC.DealerCode THEN TC.DealerCode END  AS SiteID
     , CASE WHEN TA.LocationID = TB.SiteID     THEN TB.Column3 
            WHEN TA.LocationID = TC.DealerCode THEN TC.Column3    END  AS LocationName
     , CASE WHEN TA.LocationID = TB.SiteID     THEN TB.Column4 
            WHEN TA.LocationID = TC.DealerCode THEN TC.Column4    END  AS Address1
     , CASE WHEN TA.LocationID = TB.SiteID     THEN TB.Column5 
            WHEN TA.LocationID = TC.DealerCode THEN TC.Column5    END  AS City
  FROM dbo.TableA AS TA 
LEFT OUTER 
  JOIN dbo.TableB AS TB 
    ON TB.SiteID = TA.LocationId 
LEFT OUTER 
  JOIN dbo.TableC AS TC 
    ON TC.DealerCode = TA.LocationId
ORDER 
    BY TA.Column2


I can't believe that all I needed to do was change the IN LIST SELECT statements to equality equations. This works perfectly for what I need it for. Thanks very much.

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMS SQL Development > Return columns from either table B or C based on a match in Table A

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap