Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
sudheendra.b
Starting Member
2 Posts |
Posted - 2010-02-27 : 01:19:05
|
| Hello..I am facing a problem in join the query. i qill explain my problem below in detail.we have a Product table having fields, ProductId(int), ProductName(Varchar), CategoryId(int)I have added some sample data in the product table below.ProductId ProductName CategoryId1 Maruti 12 Scoda 13 Benz 1 And also I have one more table called Descriptors having fields DescriptorValueId(int),DescriptorValue(Varchar). I have added some sample data in theDescriptors table below. DescriptorValueId DescriptorValue1 Black2 Red3 GrayI have one more table Called ProductDescriptorMapping having fields ProductId(int),DescriptorValueId(int). I have mapped ProductId and DescriptorId in the table as shown below.ProductId DescriptorValueId1 11 21 32 12 23 1My problem is I need to Select the ProductName with the combinations of DescriptorValueId as (1,2,3).I have written a join query to fetch the ProductName, but we are getting no records in the ResultSet.Below in how i have written the queryselect Distinct Product.ProductId, Product.ProductName, Product.CategoryId from Productinner join ProductDescriptorMapping on Product.ProductId = ProductDescriptorMapping.ProductId where Product.CategoryId = 1 and ProductDescriptorMapping.DescriptorValueId =1 and ProductDescriptorMapping.DescriptorValueId =2 and ProductDescriptorMapping.DescriptorValueId =3I want to select such product which is having all the three DescriptorValueIds with CategoryId=1.But no records are coming in resultset.Please help me regarding this issue.Thanks & RegardsSudheendra |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-02-27 : 01:22:16
|
| [code]select Product.ProductId, Product.ProductName, Product.CategoryId from Productinner join ProductDescriptorMapping on Product.ProductId = ProductDescriptorMapping.ProductId where Product.CategoryId = 1 and ProductDescriptorMapping.DescriptorValueId IN (1,2,3)GROUP BY Product.ProductId, Product.ProductName, Product.CategoryIdHAVING COUNT(DISTINCT ProductDescriptorMapping.DescriptorValueId) = 3[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
sudheendra.b
Starting Member
2 Posts |
Posted - 2010-02-27 : 02:05:26
|
quote: Originally posted by visakh16
select Product.ProductId, Product.ProductName, Product.CategoryId from Productinner join ProductDescriptorMapping on Product.ProductId = ProductDescriptorMapping.ProductId where Product.CategoryId = 1 and ProductDescriptorMapping.DescriptorValueId IN (1,2,3)GROUP BY Product.ProductId, Product.ProductName, Product.CategoryIdHAVING COUNT(DISTINCT ProductDescriptorMapping.DescriptorValueId) = 3 ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
Hi visak,thanks for you reply. The query is perfectly working fine.Thanks & RegardsSudheendra |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-02-27 : 02:09:42
|
welcome ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|
|