求一条SQL语句。
表A:
-----------------------------
id 分类 类名
1 a aaa
2 b bbb
3 c ccc
-----------------------------
表B:
-----------------------------
分类 数据
1 X1
1 X2
1 X3
2 Y1
2 Y2
2 Y3
3 Z1
3 Z2
3 Z3
-----------------------------
请问有没有办法用一条SQL语句从数据表 B 中取出 每个 分类的前 2 个数据?
我现在只能一次取一个分类的数据,很没效率。。。。
------解决方案--------------------select * from 表B as a where (id IN (select top 2 from 表B as b where b.分类=a.分类))
未测试 不知道行不行
------解决方案--------------------select * from B as a
where (select count(*) from B where class = a.class and data > a.data ) < 2
order by class,data DESC
------解决方案--------------------sql= "select * from 表A "
rs.open sql,ado,3,3
if not(rs.bof and rs.eof) then
do while not rs.eof
sql=select top 2 * from 表B
.........................
rs.movenext
loop
end if
------解决方案--------------------select id, A.分类, 类名, 数据 from A, (select * from B as BB where 数据 in (select top 2 数据 from B where 分类=BB.分类)) as BBB where id=BBB.分类