请大家帮帮忙了~~~~

[复制链接]
查看11 | 回复8 | 2008-5-6 11:07:48 | 显示全部楼层 |阅读模式
表m ,a是区域,b是组号,c是地域。
abc
10c233
10c237
10c541
10c545

怎么找出a,b一样,c不一样,并且将不一样的放到新建组中。例如33,37中把37放到新建组6中。sql怎么写? 谢谢~~~
回复

使用道具 举报

千问 | 2008-5-6 11:07:48 | 显示全部楼层
原帖由 michael666 于 2007-12-27 18:03 发表
表m ,a是区域,b是组号,c是地域。
abc
10c233
10c237
10c541
10c545

怎么找出a,b一样,c不一样,并且将不一样的放到新建组中。例如33,37中把37放到新建组6中。sql怎么写? 谢谢~~~

你的需求有问题。如果查你所说的,四个数据都查出来了。得有个基点。你把数据再多给一些。就明白了。
回复

使用道具 举报

千问 | 2008-5-6 11:07:48 | 显示全部楼层
什么叫33,37中把37放到新建组6中?
回复

使用道具 举报

千问 | 2008-5-6 11:07:48 | 显示全部楼层
确实不明白。楼主也没继续跟
回复

使用道具 举报

千问 | 2008-5-6 11:07:48 | 显示全部楼层
表m ,a是区域,b是组号,c是地域。
abc
10c233
10c237
10c541
10c545
需要将以上数据改成
abc
10c233
10c637
10c541
10c745
b字段不能重复,就是说一个c对应一个b.
回复

使用道具 举报

千问 | 2008-5-6 11:07:48 | 显示全部楼层
先试试
[php]
已连接。
SQL> select * from t_h_1;
A
B
C
-------------------- ---------- ----------
10c
2 33
10c
2 37
10c
5 41
10c
5 45
SQL> select a, rn1 + (select max(b) from t_h_1) b, c
2from (select a, b, c, rn, row_number() over(order by a, b) rn1
3
from (select a,
4
b,
5
c,
6
row_number() over(partition by a, b order by a, b) rn
7
from t_h_1)
8 where rn1)
9union
10select a, b, c
11from (select a,
12
b,
13
c,
14
row_number() over(partition by a, b order by a, b) rn
15
from t_h_1)
16 where rn = 1
17 order by c;
A
B
C
-------------------- ---------- ----------
10c
2 33
10c
6 37
10c
5 41
10c
7 45
SQL>
[/php]
回复

使用道具 举报

千问 | 2008-5-6 11:07:48 | 显示全部楼层
createtable T_A
(
bnumber,
cnumber);
insert into T_A values(2,33);
insert into T_A values(2,37);
insert into T_A values(5,41);
insert into T_A values(5,45);
insert into T_A values(5,47);
insert into T_A values(5,49);
insert into T_A values(3,32);
insert into T_A values(3,30);
insert into T_A values(3,39);
insert into T_A values(3,59);

select

decode(row_number() over (partition by b order by c),1,b,

max(b) over()

+row_number() over (partition by b order by c)

+rank() over (order by b)

-1

-dense_rank() over (order by b)

) new_b,

b,c
from t_a
new_b 就是你要的结果
new_b b
c
2
2
33
6
2
37
3
3
30
7
3
32
8
3
39
9
3
59
5
5
41
10
5
45
11
5
47
12
5
49
回复

使用道具 举报

千问 | 2008-5-6 11:07:48 | 显示全部楼层
试试
C:\Documents and Settings\zero>sqlplus "/ as sysdba"
SQL*Plus: Release 9.2.0.1.0 - Production on 星期五 12月 28 12:33:52 2007
Copyright (c) 1982, 2002, Oracle Corporation.All rights reserved.

连接到:
Oracle9i Enterprise Edition Release 9.2.0.1.0 - Production
With the Partitioning, OLAP and Oracle Data Mining options
JServer Release 9.2.0.1.0 - Production
SQL> conn test/1
已连接。
SQL> drop table NTEST cascade constraints;
表已丢弃。
SQL> create table NTEST
2(
3A VARCHAR2(20),
4B VARCHAR2(20),
5C VARCHAR2(20)
6);
表已创建。
SQL>
SQL> alter table NTEST disable all triggers;
表已更改。
SQL> insert into NTEST (A, B, C)
2values ('10c', '2', '33');
已创建 1 行。
SQL> insert into NTEST (A, B, C)
2values ('10c', '2', '37');
已创建 1 行。
SQL> insert into NTEST (A, B, C)
2values ('10c', '5', '41');
已创建 1 行。
SQL> insert into NTEST (A, B, C)
2values ('10c', '5', '45');
已创建 1 行。
SQL> commit;
提交完成。
SQL> alter table NTEST enable all triggers;
表已更改。
SQL>
SQL> col a for a10
SQL> col b for a10
SQL> col c for a10
SQL>
SQL> Selecta, to_char((Select Max(b) From ntest)+rn1) b,c From
2(Select a,b,c, row_number() over( Order By a,b) rn1 From (Select a,b,cFrom (Select a,b,c, row_number() over(parti
tion By a,b Order By a,b) rn From ntest )Where rn>1))
3Union
4Select a,b,c From (Select a,b,c, row_number() over(partition By a,b Order By a,b) rn From ntest )Where rn=1
5/
A
B
C
---------- ---------- ----------
10c2
33
10c5
41
10c6
37
10c7
45
SQL>
回复

使用道具 举报

千问 | 2008-5-6 11:07:48 | 显示全部楼层
A字段还有10d,10f,sql语句怎么写?
道理一样,就是说,union的第一个语句是取大的部分,union第二个语句是取小的部分。
你按照这个思路走。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行