Solved

SQL Query Join Tables

Posted on 2014-10-19
3
82 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 51

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

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…
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

631 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