动态合并多行

diaobiyong 2010-09-28 02:45:18
表T rela_id主键
rela_id struct_id file_type user_no auth_type auth_code
1951 3 01 YYY 02 01
1952 3 01 YYY 02 02
1953 3 02 YYY 02 02
1954 4 01 YYY 02 01

其中struct_id file_type user_no auth_type 都相同的数据合并成一条,auth_code只有2种状态,要么01,要么02
rela_id struct_id file_type user_no auth_type auth_code auth_code2 rela_id
1951 3 01 YYY 02 01 02 1952
1953 3 02 YYY 02 02
1954 4 01 YYY 02 01
这种动态怎么实现
...全文
105 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
minitoy 2010-09-29
  • 打赏
  • 举报
回复
auth_code如果多个的话你的结果会是什么样的?现在的结果结构没法支持多个
diaobiyong 2010-09-29
  • 打赏
  • 举报
回复
4楼方法可行的,一句SQl就搞定,不只还有没更灵活的,要是auth_code可扩展的
zhuhuadeaa 2010-09-29
  • 打赏
  • 举报
回复
这样不定行列转换,你又要显示到不同的列中,可能不好实现,如果放到一个列中还是可以的
minitoy 2010-09-29
  • 打赏
  • 举报
回复
sql是行扩展的,如果是列扩展并且列数不固定的话可以使用动态sql.直接用一个sql语句是不行了.只能使用存储过程实现.
diaobiyong 2010-09-29
  • 打赏
  • 举报
回复
auth_code有X个就意味着最多有记录是X条,这X条要合并成一条,每个X就是一列
minitoy 2010-09-28
  • 打赏
  • 举报
回复
你看这样成不
SQL> select * from tablet;

RELA_ID STRUCT_ID FILE_TYPE USER_NO AUTH_TYPE AUTH_CODE
---------- ---------- ---------- ---------- ---------- ----------
1951 3 01 YYY 02 01
1952 3 01 YYY 02 02
1953 3 02 YYY 02 02
1954 4 01 YYY 02 01

SQL>
SQL> select nvl(a.rela_id,b.rela_id) rela_id, nvl(a.struct_id,b.struct_id) struct_id,nvl(a.file_type,b.file_type) file_type, nvl(a.user_no,b.user_no) user_no,
2 nvl(a.auth_type,b.auth_type) auth_type, nvl(a.auth_code,b.auth_code) auth_code , case when a.auth_code is null then null else b.auth_code end auth_code2,
3 case when a.rela_id is null then null else b.rela_id end rela_id2
4 from (select * from tablet where auth_code='01') a full outer join (select * from tablet where auth_code='02') b
5 on a.struct_id=b.struct_id and a.file_type=b.file_type and a.user_no=b.user_no and a.auth_type=b.auth_type ;

RELA_ID STRUCT_ID FILE_TYPE USER_NO AUTH_TYPE AUTH_CODE AUTH_CODE2 RELA_ID2
---------- ---------- ---------- ---------- ---------- ---------- ---------- ----------
1951 3 01 YYY 02 01 02 1952
1954 4 01 YYY 02 01
1953 3 02 YYY 02 02

SQL>
diaobiyong 2010-09-28
  • 打赏
  • 举报
回复
把struct_id file_type user_no auth_type 字段相同的2条记录合并成一条记录
当然rela_id是主键肯定不一样,还有auth_code
一条是01 ,一条是02。这样的2条合并成一条,如果只有一条的话就不需要合并
minitoy 2010-09-28
  • 打赏
  • 举报
回复
没看懂你要什么
diaobiyong 2010-09-28
  • 打赏
  • 举报
回复
各位大牛有了解的没,期待高手

17,086

社区成员

发帖
与我相关
我的任务
社区描述
Oracle开发相关技术讨论
社区管理员
  • 开发
  • Lucifer三思而后行
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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