A表
电压等级    变电站ID  变电站名称
8           1001      1号变
8           1002      2号变
8           1003      3号变
8           1004      4号变
8           1005      5号变
8           1006      6号变
9           1007      7号变
要求这样显示8   1号变    2号变   3号变  4号变  5号变  6号变          
9   7号变
这条SQL我写了一上午了,自己知道的写法全试过了。
那位大虾路过,给小弟指点一下吧。感激不激,分不够的话,开贴再加分。十万火急!!!!求教

解决方案 »

  1.   

    http://community.csdn.net/Expert/topic/4556/4556618.xml?temp=.4353144
      

  2.   

    --将一个表里的某个字段的所有记录连接起来,返回一个字符串:
    Create Table henry_test (a varchar2(10),b int);
    Insert Into henry_test values ('aa',1);
    Insert Into henry_test values ('bb',1);
    Insert Into henry_test values ('cc',1);
    Insert Into henry_test values ('dd',2);
    Insert Into henry_test values ('ee',2);
    Insert Into henry_test values ('ff',3);
    Insert Into henry_test values ('gg',3);
    Insert Into henry_test values ('hh',3);
    Commit;
    /*
    SQL> select * from henry_test;A                                                B
    ---------- ---------------------------------------
    aa                                               1
    bb                                               1
    cc                                               1
    dd                                               2
    ee                                               2
    ff                                               3
    gg                                               3
    hh                                               38 rows selected
    */create or replace function f_henry_ConcatRowsByColumn(
    Column2Value in Varchar2,  --分组该列的值
    ColumnName1 in Varchar2,  --要连接的列名
    ColumnName2 in Varchar2,  --用来做分组依据的列名
    TableName  in Varchar2  --表名

      return varchar2 is
      v_Result varchar2(32767);
      type cur_type is ref cursor;
      myCur cur_type;
      v_Column1Value varchar2(4000);
    begin
      Open myCur for 'Select '||ColumnName1||' From '||TableName||' Where '||ColumnName2||' = '||Column2Value;
      Loop
        Fetch myCur Into v_Column1Value;
        Exit When myCur%notfound;
        v_Result:=v_Result||v_Column1Value||',';
      End Loop;
      Close myCur;
      return(v_Result);
    end /*f_henry_ConcatRowsByColumn*/;
    /
    /*
    SQL> select B,f_henry_ConcatRowsByColumn(B,'A','B','henry_test')  tt from henry_test group by B;                                      B F_HENRY_CONCATROWSBYCOLUMN(B,'
    --------------------------------------- --------------------------------------------------
                                          1 aa,bb,cc,
                                          2 dd,ee,
                                          3 ff,gg,hh,
    */
    Drop Table henry_test;
    你将脚本中的‘,’换成空格就是啦!
      

  3.   

    with adoquery1 do
    begin
    close;
    sql.clear;
    sql.add('select distinct 电压等级 from A表');
    open
    while not eof do
    begin
    i:=fieldbyname('电压等级').asinteger;
    with adoquery2 do
    begin
    close;
    sql.clear;
    sql.add('select 变电站名称 from A表 where 电压等级='+inttostr(i)');
    open;
    while not eof do
    begin
    str:=str+filedbyname('变电站名称').asstring;
    next;
    end;
     然后再把i和str存入数据库就可以了
    end;
    end;
      

  4.   

    select   电压等级,substr(max(sys_connect_by_path(变电站名称,',')),2) NAME from 
    (
      select 电压等级,变电站名称,rn,lead(rn) over(partition by 电压等级 order by rn) rn1 
      from (select 电压等级,变电站名称,row_number() over(order by 变电站ID desc) rn from a)
    )
    start with rn1 is null connect by rn1 = prior rn
    group by 电压等级