Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Need Help on SQL Query !!!

Author  Topic 

charles86
Starting Member

2 Posts

Posted - 2010-05-26 : 12:16:12
I got 1 situation that I cant solve. Can you all please help me on it ?

Table 1
Product Type Product Description
A Apple
B Banana
O Orange


Table 2
Product Type 1 Product Type 2 Product Type 3
A A B
B A O

Outcome
Product 1 Description Product 2 Description Product 3 Description
A Apple A Apple B Banana
B Banana A Apple O Orange

Here is my problem, Table 1 is a table that store the product type & its description.

Table 2 is a table that store different product type in a record.

What I need to do is , I have to select all record from Table 2 and get their description from Table 1.

Any1 can help me on how to write the SQL query ?

marjune
Starting Member

2 Posts

Posted - 2010-05-27 : 02:39:43
[code]SELECT Table1.ProductType1
,Table1.ProductType2
,Table1.ProductType3
,Table2.ProductDescription
FROM Table1
JOIN Table2 ON Table1.ProductType1 = Table2.ProductType1[/code]
Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2010-05-27 : 11:15:47
[code]-- Setup Sample Date
DECLARE @Table1 TABLE (ProductType CHAR(1), ProductDescription VARCHAR(50))
INSERT @Table1
SELECT 'A', 'Apple'
UNION ALL SELECT 'B', 'Banana'
UNION ALL SELECT 'O', 'Orange'

DECLARE @Table2 TABLE (ProductType1 CHAR(1), ProductType2 CHAR(1), ProductType3 CHAR(1))
INSERT @Table2
SELECT 'A', 'A', 'B'
UNION ALL SELECT 'B', 'A', 'O'

-- Run Query
SELECT
Table2.ProductType1,
A.ProductDescription AS ProductDescription1,
Table2.ProductType2,
B.ProductDescription AS ProductDescription2,
Table2.ProductType3,
C.ProductDescription AS ProductDescription2
FROM
@Table2 AS Table2
INNER JOIN
@Table1 AS A
ON Table2.ProductType1 = A.ProductType
INNER JOIN
@Table1 AS B
ON Table2.ProductType2 = B.ProductType
INNER JOIN
@Table1 AS C
ON Table2.ProductType3 = C.ProductType[/code]
Go to Top of Page

charles86
Starting Member

2 Posts

Posted - 2010-05-30 : 10:35:39
thanks lamprey , i will go to try it out. hope it can work. thx a lot
Go to Top of Page
   

- Advertisement -