Solved

SQL Query Join Tables

Posted on 2014-10-19
3
78 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 46

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:ScottPletcher
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

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

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

Suggested Solutions

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

910 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now