MS SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
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:
Generate data entry and reporting .NET Web apps in minutes, straight from your database. Read our FREE whitepaper “Build Web 2.0 Applications Without Hand-Coding” Download now!
  #1  
Old August 20th, 2004, 05:41 PM
iRead iRead is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2004
Posts: 14 iRead User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Help with a SELECT statement

I have 4 tables. The first table contains details for a customer order. The second contains inventory items. The third contains alternate names for these items specific to the customer. The fourth holds the relationship between the items and the alternate name specific to the customer. Now with one SELECT statement I want to be able to return two columns. The first column will contain the Identity field from table two. The second column will contain the name field from table two unless there are matching records in table three as determined in the relationship established in table four. I the case there are matching records in table three then column two should contain the contents of the name field from table three. I now have a SELECT statement with as series of INNER and LEFT OUTER JOINS that returns three columns. The first is the identity fields. The second is the name field from table two. The third is the name field from table three. If I could somehow get the third column into column two where column three isn’t NULL and return this dataset I’d have my problem solved. All of this done with one SELECT statement. If I can’t do this in one SELECT statement I’ll have to do with code behind (not how I would prefer to do this).

SELECT DISTINCT
StandardItem.[Id], StandardItem.[Name], AltItem.[Name] AS AltName
FROM StandardItem
INNER JOIN Orders ON StandardItem.[Id] = ItemId
AND Orders.payorID = 3
LEFT OUTER JOIN ItemMap ON ItemId = StandardItem.[Id]
LEFT OUTER JOIN AltItem ON AltItem.[Id] = ItemMap.AltItemId


Any suggestions will be greatly appreciated

Thank you,
iRead

Reply With Quote
  #2  
Old August 20th, 2004, 06:02 PM
Olijames Olijames is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2004
Posts: 1 Olijames User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Quote:
Originally Posted by iRead
I have 4 tables. The first table contains details for a customer order. The second contains inventory items. The third contains alternate names for these items specific to the customer. The fourth holds the relationship between the items and the alternate name specific to the customer. Now with one SELECT statement I want to be able to return two columns. The first column will contain the Identity field from table two. The second column will contain the name field from table two unless there are matching records in table three as determined in the relationship established in table four. I the case there are matching records in table three then column two should contain the contents of the name field from table three. I now have a SELECT statement with as series of INNER and LEFT OUTER JOINS that returns three columns. The first is the identity fields. The second is the name field from table two. The third is the name field from table three. If I could somehow get the third column into column two where column three isn’t NULL and return this dataset I’d have my problem solved. All of this done with one SELECT statement. If I can’t do this in one SELECT statement I’ll have to do with code behind (not how I would prefer to do this).

SELECT DISTINCT
StandardItem.[Id], StandardItem.[Name], AltItem.[Name] AS AltName
FROM StandardItem
INNER JOIN Orders ON StandardItem.[Id] = ItemId
AND Orders.payorID = 3
LEFT OUTER JOIN ItemMap ON ItemId = StandardItem.[Id]
LEFT OUTER JOIN AltItem ON AltItem.[Id] = ItemMap.AltItemId


Any suggestions will be greatly appreciated

Thank you,
iRead


This only my 10 cents worth but i am sure that you need to put all the tables you wish to query from in the FROM part of the select statement.

So you would have

SELECT DISTINCT
StandardItem.[Id], StandardItem.[Name], AltItem.[Name] AS AltName
FROM StandardItem, Orders, ItemMap, AltItem
INNER JOIN Orders ON StandardItem.[Id] = ItemId
AND Orders.payorID = 3
LEFT OUTER JOIN ItemMap ON ItemId = StandardItem.[Id]
LEFT OUTER JOIN AltItem ON AltItem.[Id] = ItemMap.AltItemId

But i could be wrong.

Oli.

Reply With Quote
  #3  
Old August 21st, 2004, 08:03 AM
r937's Avatar
r937 r937 is offline
SQL Consultant
Dev Shed God 25th Plane (17000 - 17499 posts)
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 17,331 r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 3 Weeks 5 Days 7 h 21 m 33 sec
Reputation Power: 891
oli, sorry, yes, you are

iread, i've read your question a couple of times and just don't get it

Quote:
Now with one SELECT statement I want to be able to return two columns. The first column will contain the Identity field from table two. The second column will contain the name field from table two unless there are matching records in table three as determined in the relationship established in table four. I the case there are matching records in table three then column two should contain the contents of the name field from table three. I now have a SELECT statement with as series of INNER and LEFT OUTER JOINS that returns three columns. The first is the identity fields. The second is the name field from table two. The third is the name field from table three. If I could somehow get the third column into column two where column three isn’t NULL and return this dataset I’d have my problem solved.
you may have to show the relationships by displaying a few rows from each table
__________________
r937.com | rudy.ca

Reply With Quote
  #4  
Old August 23rd, 2004, 11:12 AM
iRead iRead is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2004
Posts: 14 iRead User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
I’ve figured it out!

Quote:
Originally Posted by r937
oli, sorry, yes, you are

iread, i've read your question a couple of times and just don't get it

you may have to show the relationships by displaying a few rows from each table


I came up with this:

SELECT DISTINCT
StandardItem.[Id], StandardItem.[Name]
FROM StandardItem
INNER JOIN Orders ON StandardItem.[Id] = ItemId
AND Orders.payorID = 3
LEFT OUTER JOIN ItemMap ON ItemId = StandardItem.[Id]
LEFT OUTER JOIN AltItem ON AltItem.[Id] = ItemMap.AltItemId\
WHERE AltItem.[Name] IS NULL
UNION
SELECT DISTINCT
StandardItem.[Id], AltItem.[Name]
FROM StandardItem
INNER JOIN Orders ON StandardItem.[Id] = ItemId
AND Orders.payorID = 3
LEFT OUTER JOIN ItemMap ON ItemId = StandardItem.[Id]
LEFT OUTER JOIN AltItem ON AltItem.[Id] = ItemMap.AltItemId\
WHERE AltItem.[Name] IS NOT NULL

Works brilliantly! Thank you for your kind input.

iRead

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMS SQL Development > Help with a SELECT statement


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 | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway