Solved

SQL Query Join Tables

Posted on 2014-10-19
3
76 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
Comment Utility
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 45

Expert Comment

by:Vitor Montalvão
Comment Utility
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
Comment Utility
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

771 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

11 Experts available now in Live!

Get 1:1 Help Now