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 |
vaibhavktiwari83
Aged Yak Warrior
843 Posts |
Posted - 2010-12-03 : 04:27:40
|
i have a table in my database which maps two tables products and category the table has three columns 1...id2...product_id3...category_ida product can be associated with many categoriesthe query should take a list of category_id as input and output the distinct product_id which are associated with all of these categories.eg in my table i have the following data id product_id category_id1 1 12 1 23 1 34 2 15 2 26 2 47 2 5now if i give 1,2,3 as input to query it should output me product_id = 1if i give 1,2 as input to query it should output me product_id = 1,2Vaibhav TIf I cant go back, I want to go fast... |
|
vaibhavktiwari83
Aged Yak Warrior
843 Posts |
Posted - 2010-12-03 : 04:29:04
|
Sample Data and Query what I tried - Is there any other way to do this - Create table #Product (id int primary key,product_id int,category_id int)insert #Product select 1,1,1union allselect 2,1,2union allselect 3,1,3union allselect 4,2,1union allselect 5,2,2union allselect 6,2,4union allselect 7,2,5DECLARE @Catid1 int, @Catid2 int, @Catid3 intSELECT @Catid1=1,@Catid2=2,@Catid3=3(SELECT distinct product_idFROM #Productwhere category_id=@Catid1) intersect (SELECT distinct product_idFROM #Productwhere category_id=@Catid2) intersect (SELECT distinct product_idFROM #Productwhere category_id=@Catid3) Vaibhav TIf I cant go back, I want to go fast... |
 |
|
vaibhavktiwari83
Aged Yak Warrior
843 Posts |
Posted - 2010-12-04 : 02:09:16
|
Any suggestion ?Vaibhav TIf I cant go back, I want to go fast... |
 |
|
|
|
|
|
|