Solved

SQL Query Join Tables

Posted on 2014-10-19
3
79 Views
Last Modified: 2014-10-20
I have two tables, which i need to run a query against to extract a distinct list from.

The tables are as follows:

tblProductVariants
ID,intProductID,txtVariant
1,1,Size
2,2,Size
3,2,Colour
4,3,Size
5,4,Size
6,4,Colour


tblProductConfigurations
ID,txtConfiguration,intVariantID,intProductID
1,Small,1,1
2,Large,1,1
3,XL,1,1
4,Small,2,2
5,Large,2,2
6,XL,2,2
7,Red,3,2
8,Blue,3,2
9,Green,3,2
10,White,3,2

Im trying to figure out the query syntax to allow me to get a distinct list of sizes (small/large/XL) where the txtVariant in tblProductVariants = Size

I will also need to do the same for colours, whereby i need a distinct list of colours where the txtVariant = Colour.

Can anybody help?
0
Comment
Question by:net-workx
3 Comments
 
LVL 15

Accepted Solution

by:
Haris Djulic earned 500 total points
ID: 40391058
Hello,

here is the code for sizes :

select distinct txtConfiguration as sizes 
from tblProductConfigurations t left join tblProductVariants tt on t.intVariantID=tt.id and t.intProductID=tt.intProductID
where txtVariant='Size'

Open in new window


and here is the code for colors:

select distinct txtConfiguration as sizes 
from tblProductConfigurations t left join tblProductVariants tt on t.intVariantID=tt.id and t.intProductID=tt.intProductID
where txtVariant='Color'

Open in new window


All can be tested here: http://sqlfiddle.com/#!3/54d97/2
0
 
LVL 47

Expert Comment

by:Vitor Montalvão
ID: 40391447
It's better to work with ID's since they are usually indexed columns and you can avoid typos:

SELECT DISTINCT C.txtConfiguration
FROM tblProductConfigurations C
	INNER JOIN tblProductVariants V ON (C.intVariantID = V.ID)
WHERE V.ID=1 -- Size

SELECT DISTINCT C.txtConfiguration
FROM tblProductConfigurations C
	INNER JOIN tblProductVariants V ON (C.intVariantID = V.ID)
WHERE V.ID=3 -- Colour

Open in new window

0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 40393162
select distinct pc.txtConfiguration as sizes
from tblProductConfigurations pc
inner join tblProductVariants pv on
    pc.intVariantID=pv.id and
    pc.intProductID=pv.intProductID
where
    pv.txtVariant='Size'
   
select distinct pc_size.txtConfiguration as sizes
from tblProductConfigurations pc
inner join tblProductVariants pv on
    pc.intVariantID=pv.id and
    pc.intProductID=pv.intProductID
inner join tblProductConfigurations pc_size on
    pc_size.intVariantID = (SELECT TOP (1) pv_size.ID FROM tblProductVariants pv_size WHERE pv_size.intProductID = pv.intProductID and pv_size.txtVariant = 'Size') and
    pc_size.intProductID=pv.intProductID
where
    pv.txtVariant='Color'
0

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Suggested Solutions

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

786 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