Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 84
  • Last Modified:

SQL Query Join Tables

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
net-workx
Asked:
net-workx
1 Solution
 
Haris DjulicCommented:
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
 
Vitor MontalvãoMSSQL Senior EngineerCommented:
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
 
Scott PletcherSenior DBACommented:
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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now