查询结果如下:name    re   count张三    红包     10
张三    奖金     20
张三    工资     100
张三    支出     500
李四    游泳     5
李四    爬山     7
李四    旅游     50
王五    吃饭     50其中,re项最多4中情况,而且re的内容不固定想得到如下结果:
name   re1 count1 re2  count2 re3  count3 re4  count4
张三   红包     10     奖金     20     工资     100    支出     500
李四   游泳     5      爬山     7      旅游     50
王五   吃饭     50求一个sql,在线等高人。

解决方案 »

  1.   

    动态行转列http://topic.csdn.net/u/20100109/13/6a10c168-f190-4766-b838-adbf03c4ac7b.html?54035http://topic.csdn.net/u/20100109/13/6a10c168-f190-4766-b838-adbf03c4ac7b.html?54035
      

  2.   

    给个简单的,select t.name,
    max(decode(rn,1,t.re)) as re1,max(decode(rn,1,t.count)) as count1,
    max(decode(rn,2,t.re)) as re1,max(decode(rn,2,t.count)) as count1,
    max(decode(rn,3,t.re)) as re1,max(decode(rn,3,t.count)) as count1,
    max(decode(rn,4,t.re)) as re1,max(decode(rn,4,t.count)) as count1
    from (
    select t.*,row_number() over(partition by t.name order by t.name) rn  
    from sal t
    )t
    group by t.name
    结果:
    NAME REMARK1 COUNT1 REMARK1 COUNT1 REMARK1 COUNT1 REMARK1 COUNT1
    李四 游泳 5
    张三  奖金  20 支出 50 红包  10 工资 100
      

  3.   


    --这种帖子很多了,而且也有精华帖,楼主看下一楼的链接吧,、
    --这里给你写个:
    --过程:
    create or replace procedure row_to_col_func(cur out sys_refcursor)
    as
      sqlstr varchar2(4000):='select name';
    begin
         for i in 1..4 loop
           sqlstr:=sqlstr||chr(10)||','||'max(decode(rn,'''||i||''',re,'''')) as re'||i;
           sqlstr:=sqlstr||chr(10)||','||'max(decode(rn,'''||i||''',count,'''')) as count'||i;
         end loop ;
         sqlstr:=sqlstr||chr(10)||'from (select a.*,row_number()over(partition by name order by name) rn from tab a) ';
         sqlstr:=sqlstr||chr(10)||'group by name ' ;     open cur for sqlstr;
    end row_to_col_func;--测试:
    SQL*Plus: Release 8.0.6.0.0 - Production on 星期四 5月 26 12:35:02 2011(c) Copyright 1999 Oracle Corporation.  All rights reserved.
    Connected to:
    Oracle Database 10g Release 10.1.0.2.0 - ProductionSQL> set serveroutput on
    SQL> set linesize 30000
    SQL> select * from tab;NAME                 REMARK                    COUNT
    -------------------- -------------------- ----------
    张三                 红包                         10
    张三                 奖金                         20
    张三                 工资                        100
    张三                 支出                        500
    李四                 游泳                          5
    李四                 爬山                          7
    李四                 旅游                         50
    王五                 吃饭                         508 rows selected.SQL> var cur refcursor
    SQL> exec row_to_col_func(:cur);PL/SQL procedure successfully completed.SQL> print curNAME    REMARK1    COUNT1     REMARK2    COUNT2     REMARK3    COUNT3   REMARK4   COUNT4
    ------- ---------- ---------- ---------- ---------- ---------- -------  --------  -----
    张三    红包       10         奖金       20         工资       100      支出      500 
    李四    游泳        5         爬山        7         旅游       50
    王五    吃饭       50SQL>