首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

小弟不才,请问sql高手,给高分

2011-12-30 
小弟不才,请教sql高手,给高分表的结构是idvalue1001100210052002200430023003400140024004500150036002600

小弟不才,请教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


*/

热点排行