日期:2014-05-19  浏览次数:20566 次

小弟不才,请教sql高手,给高分
表的结构是
id         value
100       1
100       2
100       5
200       2
200       4
300       2
300       3
400       1
400       2
400       4
500       1
500       3
600       2
600       3
700       1
700       3
问题是:如何选出同时都包括1和2的id的数目
即类似
100       1
100       2
400       1
400       2
即同时包括1和2的id有两个
小弟实现关联规则算法,需要用到这个
该怎么做啊,想好久了,大家帮帮忙,先谢谢了


------解决方案--------------------
--借用楼上数据

--加上排序

declare @tb table(id int, value int)
insert into @tb select 100,1
union all select 100,2
union all select 100,5
union all select 200,2
union all select 200,4
union all select 300,2
union all select 300,3
union all select 400,1
union all select 400,2
union all select 400,4
union all select 500,1
union all select 500,3
union all select 600,2
union all select 600,3
union all select 700,1
union all select 700,3


select *
from (

select A.*
from
(select * from @tb where value=1) A
inner join (select * from @tb where value=2) B on A.id=B.id

union

select B.*
from
(select * from @tb where value=1) A
inner join (select * from @tb where value=2) B on A.id=B.id

) T

order by id, value

/*

100 1
100 2
400 1
400 2


*/