tags:

views:

59

answers:

4
Model   Icolor

m1  i1

m1  i2

m1  i3

m2  i2

m2  i3

m2  i4

I am having a table like this, i need to select Icolor values which is common to both model m1 and m2.

A: 
select lcolor from Model where m1 = x
union 
select lcolor from Model where m2 = y

x is your model m1 and y is your model m2 value or if it is a bit datatype then you've got a 1 there. union gets you the shared values from the results of both queries. I'm not sure if the syntax is correct (but I'm pretty sure it is). If not you can google it.

Gambrinus
+1  A: 
DECLARE @TABLE TABLE
(
    Model VARCHAR(30),
    LColour VARCHAR(30)
)

INSERT INTO @TABLE 
SELECT 'm1','i1' UNION
SELECT 'm1','i2' UNION
SELECT 'm1','i3' UNION
SELECT 'm2','i2' UNION
SELECT 'm2','i3' UNION
SELECT 'm2','i4' 

SELECT 
    LColour
FROM 
    @TABLE
GROUP BY 
    LColour
HAVING 
    COUNT(*) = (SELECT COUNT(DISTINCT Model) FROM @TABLE)

Edited to give all colours that are common to every model :)

Meff
Interesting solution. But what about performance? (it's not related to the question but it's rather interesting for me)
Roman
Performance should be acceptable, this is (So far as I know) the best way to achieve this, as there are no self-joins or unions used. This table could be indexed for this query, but without a real schema it's difficult to suggest. Data distribution also plays a part.I've used similar approaches in the past, where I had to find an ID in a table that matched at least four out of possibly hundreds values, and found HAVING COUNT(*) = 4 worked very well for me :)
Meff
I made some performance tests. It's not too slow but my solution is faster in 5 times (and I believe that my solution is also not the fastest possible).
Roman
Hi Meff,The table i am having is having many models, i have to select a color which is common to all models.
Pugazh
Edited to fulfil your requirements ;)
Meff
A: 

UPDATED:

select color from 
  (select color, T2.num from test_table, 
     (select count(*) as num from (select distinct model from test_table) T) T2
group by color, T2.num
having count(*) = T2.num) T3
Roman
Hi Roman, The table i am having is having many models, i have to select a color which is common to all models.
Pugazh
Hi Roman, you said your initial solution was faster than mine, but haven't you now turned yours into a correlated subquery? That'll give a new query per row, making mine faster surely :p
Meff
I wrote my comment when we assumed that only 2 models exist. Now I'd prefer your solution. but anyway no one cares.
Roman
A: 

I beleive it shuld be something like this:

SELECT tt.Icolor
FROM test_table tt
INNER JOIN test_table tt1 ON tt1.Icolor = tt.Icolor AND tt1.Model = 'm2'
WHERE tt.Model = 'm1'
krig
Hi Krig,The table i am having is having many models, i have to select a color which is common to all models.
Pugazh
I'm doing almost the same for my application. I'm preparing query from user input by adding INNER JOIN for each additional item. INNER JOIN test_table ttN ON ttN.Icolor = tt.Icolor AND ttN.Model = 'mN' I don't know what technology you are using but this is very simple solution for PHP. And performance is good for this query with indexes on Model and Icolor.
krig