求助,我想在查找出来的结果集中指定行加空行,怎么办?

ccpud 2010-12-30 04:02:59
比如数据库数据是这样的
a b
22 11
22 31
22 21
34 22
23 32
23 23
23 12
34 13
a列是组号,b列的号是唯一的。我想要的效果是,a列值相等的放到一起,然后空一格,再放下一组的数据。
比如
22 11
22 21
22 31

23 32
23 12
23 23

34 22
34 13

另外一个问题。a列是组号,我想要的效果是,b列有Z个值,我就从1到Z给它分好。
比如
22 11 1
22 21 2
22 31 3

23 32 1
23 12 2
23 23 3

34 22 1
34 13 2

每组都从1开始。
...全文
125 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
ccpud 2010-12-30
  • 打赏
  • 举报
回复
谢谢Oraclefans_。也谢谢爱新觉罗.毓华和Phoenix_99。因为发帖比较急,给的分不多,所以全给Oraclefans_了。再次感谢各位
Phoenix_99 2010-12-30
  • 打赏
  • 举报
回复
第一个:
with temp as(
select 22 a,11 b from dual
union all
select 22 a,31 b from dual
union all
select 22 a,21 b from dual
union all
select 34 a,22 b from dual
union all
select 23 a,32 b from dual
union all
select 23 a,23 b from dual
union all
select 23 a,12 b from dual
union all
select 34 a,13 b from dual
)
select case when b is null then null else a end a,b from(
select a,b,max(a) c from(
select a, b from temp
union all
select a,null b from temp group by a
) group by a,b
) order by c,a

第二个:
select t.* , row_number() over(partition by a order by b) from tb t
Oraclefans_ 2010-12-30
  • 打赏
  • 举报
回复

--------这样可以不
SQL> with tab as
2 (
3 select 22 a, 11 b from dual union all
4 select 22 a, 31 b from dual union all
5 select 22 a, 21 b from dual union all
6 select 34 a, 22 b from dual union all
7 select 23 a, 32 b from dual union all
8 select 23 a, 23 b from dual union all
9 select 23 a, 12 b from dual union all
10 select 34 a, 13 b from dual
11 )
12 select decode(b, null, '-----------', a) a,
13 decode(b, null, '-----------', b),
14 decode(b, null, '-----------', rn) rn
15 from (select a, b, row_number() over(partition by a order by b) rn
16 from tab
17 group by rollup(a, b))
18 ;

A DECODE(B,NULL,'-----------',B) RN
---------------------------------------- ---------------------------------------- ----------------------------------------
22 11 1
22 21 2
22 31 3
----------- ----------- -----------
23 12 1
23 23 2
23 32 3
----------- ----------- -----------
34 13 1
34 22 2
----------- ----------- -----------
----------- ----------- -----------

12 rows selected

SQL>
dawugui 2010-12-30
  • 打赏
  • 举报
回复
select t.* , row_number() over(partition by a order by b) from tb t

17,377

社区成员

发帖
与我相关
我的任务
社区描述
Oracle 基础和管理
社区管理员
  • 基础和管理社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧