首页 | 编程语言 | 网站建设 | 游戏天堂 | 冲浪宝典 | 网络安全 | 操作系统 | 软件时空 | 硬件指南 | 病毒相关 | IT 认证
软讯网络 > 冲浪宝典 > 网络资源 > 利用Oracle rownum完成行转列一例
【标  题】:利用Oracle rownum完成行转列一例
【关键字】:Oracle,rownum
【来  源】:http://blog.csdn.net/pengpenglin/archive/2007/01/07/1476118.aspx

利用Oracle rownum完成行转列一例

利用Oracle rownum完成行转列一例

 

一、需求背景:

 

ACREATE TABLE T_SMCOMMONMSG (

        FXH             NUMBER,

        FTYPE           NUMBER,

        FMSG_CONTENT  VARCHAR2(1024 BYTE) 

)

 

BCREATE TABLE T_SMYL_COMMONSND (

        FXH               NUMBER,

        FMSG_CONTENT1  VARCHAR2(1024 BYTE),

        FMSG_CONTENT2  VARCHAR2(1024 BYTE)        

)

 

A中的原始记录如下:

    FXH     FTYPE   FMSG_CONTENT

------  -------- --------------

   98       0        msg1

   99       0        msg2

  100       0        msg3

  101       0        msg4

 

B中的现有记录如下:

    FXH     FMSG_CONTENT1   FMSG_CONTENT2

  ------- ---------------- --------------

1                        

2                        

 

需求:现要求将表A中的记录转换成如下格式的表B中的记录:

FXH     FMSG_CONTENT1   FMSG_CONTENT2  

  ------  ---------------- -------------- 

1          msg1           msg2          

2          msg3           msg4

 

注:表A是一个包含了多种FTYPE类型的信息表,其中FTYPE=0的信息就是转换的原始信息,序号从98开始

B是一个包含了一种FTYPE类型的信息表,其序号从1开始

 

 

二、解决方案:

 

1.尝试使用如下SQL语句:

    update t_smyl_commonsnd a

     set a.fmsg_content1 = (select fmsg_content

                                from t_smcommonmsg b

                             where b.ftype = 0

                                and mod(b.fxh,2) =0)

 

 

 

错误原因:子查询的结果是一个结果集,不能将结果集赋給一条记录的某个字段

 

2.尝试使用如下SQL语句:

    update t_smyl_commonsnd a

      set a.fmsg_content1 = (select fmsg_content

                                 from t_smcommonmsg b

                            where b.ftype = 0

                              and mod(b.fxh,2) =0

                              and a.fxh = b.fxh -97) 

   

   

   

    但执行: select * from t_smyl_commonsnd;结果为:

        FXH     FMSG_CONTENT1   FMSG_CONTEN2

--------  --------------- --------------

1                                       msg1       

2      

3           msg2

4          

5           msg3

   

    错误原因:子查询中对与表AFXH值为偶数的记录,其返回值为NULL,所以偶数行的字段都为空

 

3.尝试使用如下SQL语句:

    --更新字段:FMSG_CONTENT1

    update t_smyl_commonsnd a

      set a.fmsg_content1 = (select fmsg_content

                              from (select rownum id, fmsg_content

                                          from (select fmsg_content

                                                 from t_smcommonmsg

                                                where ftype = 0

                                                  and mod(fxh, 2) = 0

                                                order by fxh asc) v1

                                             ) v2

                                Where a.fxh = v2.id);

 

 

 

 

 

     --更新字段:FMSG_CONTENT2

       update t_smyl_commonsnd a

      set a.fmsg_content2 = (select fmsg_content

                               from (select rownum id, fmsg_content

                                         from (select fmsg_content

                                                 from t_smcommonmsg

                                                where ftype = 0

                                                  and mod(fxh, 2) = 1

                                                order by fxh asc) v1

                                             ) v2

                                where a.fxh = v2.id);

      返回结果如下:

      FXH   FMSG_CONTENT1   FMSG_CONTENT2

----- --------------- ---------------

  1       msg1              msg2

       2       msg3              msg4

   

   更新成功!   

 

      分析:

            1). 找出表A中所有FTYPE=0FXH为双数的记录,也即是FMSG_CONTENT1字段的目标值

select fmsg_content

                   from t_smcommonmsg

              where ftype = 0

                    and mod(fxh, 2) = 0

                  order by fxh asc

 

           2).給筛选的结果加上Rownum,和表B中的记录一一对应

                select rownum id, fmsg_content

                  from (select fmsg_content

                            from t_smcommonmsg

                          where ftype = 0

                             and mod(fxh, 2) = 0

                           order by fxh asc) v1

 

           3).找出视图V1ID值和表BFXH号值对应的记录

                select fmsg_content

                   from (select rownum id, fmsg_content

                            from (select fmsg_content

                                     from t_smcommonmsg

                                    where ftype = 0

                                      and mod(fxh, 2) = 0

                                    order by fxh asc) v1

                                   ) v2

                 where a.fxh = v2.id

      

           

 

            4).更新记录的值:

                update t_smyl_commonsnd a

                    set a.fmsg_content1 = (select fmsg_content

                                                 from (select rownum id, fmsg_content

                                                          from (select fmsg_content

                                                                   from t_smcommonmsg

                                                                  where ftype = 0

                                                                    and mod(fxh, 2) = 0

                                                                  order by fxh asc) v1

                                                        ) v2

                                               where a.fxh = v2.id);

 
Oracle数据文件位置迁移笔记:【上一篇】
SQL语句优化技术分析:【下一篇】
【相关文章】
  • Oracle数据文件位置迁移笔记
  • ORACLE中的日志值转换成time_t后,与C语言中的time_t相差8小时
  • oracle时间操作
  • 如何迁移Oracle的数据文件
  • Oracle数据库入门心得
  • SQL92,SQL SERVER,ORACLE,DB2可更新视图概述
  • 对比mysql oracle db2 的部分ddl语法
  • oracle中的切分函数
  • 用户如何有效地利用oracle数据字典
  • Oracle优化
  • 【随机文章】
  • 枚举BIG5中的汉字
  • Code Perversion
  • 重装系统怎样恢复GRUB启动
  • HTML之多媒体
  • 递归函数引用参数对递归工作栈大小的影响分析
  • 取MAC地址
  • UNIX家庭小点滴
  • 网上邻居网络共享双机互连
  • 侠客系统修改器1.21 算法分析
  • cygwin中文输入和显示
  • 【相关评论】
    没有相关评论
    【发表评论】
    姓名:
    邮件:
    随机码*
    评论*
          
    |  首 页  |  版权声明  |  联系我们   |  网站地图  |
    CopyRight © 2004-2007 软讯网络 All Rigths Reserved.