如何查询出第x1到第x2组的数据,组是按b分组的,注意,是组,不是行!
22 个解决方案
#1
帮顶一下~我也很期待答案呐!
#2
能写成什么人都看懂你说的吗
#3
没看明白,给个示例数据吧
#4
木看明白..
#5
使用临时表。
#6
能看懂的楼主就应该给50了
写出来的话楼主应该给500
写出来的话楼主应该给500
#7
第x1到第x2组 清说的明白点。
#8
select * from xx
join
(
select top @x2 b from xx
where b not in
(
select top @x1 b from xx group by b
)
group by b
)tmp on xx.b=tmp.b
#9
????
#10
不好意思各位,就是说
a b c
1 2 3
4 2 5
3 3 3
2 1 3
2 3 6
4 1 7
8 4 1
9 4 6
4 3 2
4 3 6
3 2 4
4 3 3
我想找出
1 2 3
3 2 4
4 2 5
4 3 2
4 3 6
2 3 6
3 3 3
4 3 3
a b c
1 2 3
4 2 5
3 3 3
2 1 3
2 3 6
4 1 7
8 4 1
9 4 6
4 3 2
4 3 6
3 2 4
4 3 3
我想找出
1 2 3
3 2 4
4 2 5
4 3 2
4 3 6
2 3 6
3 3 3
4 3 3
#11
就是说select * from xx group by b后我想找第3到11组的数据
#12
我来
#13
select * from t where t.b = 2 or t.b = 3 order by b
#14
Oracle 中可以用 rank 分析函数:
select *
from
(
select a, b, c, rank() over (partion by b) as rnk from xx
) t
where t.rnk >= 3 and t.rnk <= 11
select *
from
(
select a, b, c, rank() over (partion by b) as rnk from xx
) t
where t.rnk >= 3 and t.rnk <= 11
#15
declare @tb table (a int,b int,c int)
insert into @tb select 1,2,3
insert into @tb select 4,2,5
insert into @tb select 3,3,3
insert into @tb select 2,1,3
insert into @tb select 2,3,6
insert into @tb select 4,1,7
insert into @tb select 8,4,1
insert into @tb select 9,4,6
insert into @tb select 4,3,2
insert into @tb select 4,3,6
insert into @tb select 3,2,4
insert into @tb select 4,3,3
select * from @tb a
where exists(
select 1 from @tb where b=a.b
group by b having count(1)>2)
order by b,a
a b c
1 2 3
3 2 4
4 2 5
2 3 6
3 3 3
4 3 2
4 3 6
4 3 3
排序没排好 soryy
#16
MS SQL 2005 也有相同的函数。
#17
是这意思不,下班了,晚上看
#18
select *
from
(
select a, b, c, rank() over (order by b) as rnk from xx
) t
where t.rnk > = 3 and t.rnk <= 11
这样就可以了。
#19
up
#20
感谢各位的帮忙,特别是fangxinggood.应该用DENSE_RANK()可以解决,
#21
顶
#22
select *
from
(
select a, b, c, rank() over (order by b) as rnk from xx
) t
where t.rnk > = 3 and t.rnk <= 11
from
(
select a, b, c, rank() over (order by b) as rnk from xx
) t
where t.rnk > = 3 and t.rnk <= 11
#1
帮顶一下~我也很期待答案呐!
#2
能写成什么人都看懂你说的吗
#3
没看明白,给个示例数据吧
#4
木看明白..
#5
使用临时表。
#6
能看懂的楼主就应该给50了
写出来的话楼主应该给500
写出来的话楼主应该给500
#7
第x1到第x2组 清说的明白点。
#8
select * from xx
join
(
select top @x2 b from xx
where b not in
(
select top @x1 b from xx group by b
)
group by b
)tmp on xx.b=tmp.b
#9
????
#10
不好意思各位,就是说
a b c
1 2 3
4 2 5
3 3 3
2 1 3
2 3 6
4 1 7
8 4 1
9 4 6
4 3 2
4 3 6
3 2 4
4 3 3
我想找出
1 2 3
3 2 4
4 2 5
4 3 2
4 3 6
2 3 6
3 3 3
4 3 3
a b c
1 2 3
4 2 5
3 3 3
2 1 3
2 3 6
4 1 7
8 4 1
9 4 6
4 3 2
4 3 6
3 2 4
4 3 3
我想找出
1 2 3
3 2 4
4 2 5
4 3 2
4 3 6
2 3 6
3 3 3
4 3 3
#11
就是说select * from xx group by b后我想找第3到11组的数据
#12
我来
#13
select * from t where t.b = 2 or t.b = 3 order by b
#14
Oracle 中可以用 rank 分析函数:
select *
from
(
select a, b, c, rank() over (partion by b) as rnk from xx
) t
where t.rnk >= 3 and t.rnk <= 11
select *
from
(
select a, b, c, rank() over (partion by b) as rnk from xx
) t
where t.rnk >= 3 and t.rnk <= 11
#15
declare @tb table (a int,b int,c int)
insert into @tb select 1,2,3
insert into @tb select 4,2,5
insert into @tb select 3,3,3
insert into @tb select 2,1,3
insert into @tb select 2,3,6
insert into @tb select 4,1,7
insert into @tb select 8,4,1
insert into @tb select 9,4,6
insert into @tb select 4,3,2
insert into @tb select 4,3,6
insert into @tb select 3,2,4
insert into @tb select 4,3,3
select * from @tb a
where exists(
select 1 from @tb where b=a.b
group by b having count(1)>2)
order by b,a
a b c
1 2 3
3 2 4
4 2 5
2 3 6
3 3 3
4 3 2
4 3 6
4 3 3
排序没排好 soryy
#16
MS SQL 2005 也有相同的函数。
#17
是这意思不,下班了,晚上看
#18
select *
from
(
select a, b, c, rank() over (order by b) as rnk from xx
) t
where t.rnk > = 3 and t.rnk <= 11
这样就可以了。
#19
up
#20
感谢各位的帮忙,特别是fangxinggood.应该用DENSE_RANK()可以解决,
#21
顶
#22
select *
from
(
select a, b, c, rank() over (order by b) as rnk from xx
) t
where t.rnk > = 3 and t.rnk <= 11
from
(
select a, b, c, rank() over (order by b) as rnk from xx
) t
where t.rnk > = 3 and t.rnk <= 11