oracle显示表像笛卡尔坐标系

时间:2016-08-04 08:14:28

标签: oracle oracle11g system coordinate cartesian

我有一张桌子

create table test_table(
id number(10),
x number(10),
y number(10),
svalue number(10));

填写表格

declare
    i integer;
begin
    i := 0;
    for x in 1 .. 10 loop
        for y in 1 .. 10 loop
            i := i + 1;
            insert into test_table
                (Id, x, y, svalue)
            values
                (i, x, y, x + y);
        end loop;
    end loop;
    commit;
end;

我怎样才能显示像

这样的表格
    1 2 3 4 5 Ny
  1 2 3 4 5 6 
  2 3 4 5 6 7
  3 4 5 6 7 8
  Nx

其中x - 行,y - 列,svalue - 值x,y

2 个答案:

答案 0 :(得分:1)

听到我使用循环test_table实现笛卡尔坐标系。

declare
    HEAD VARCHAR2(100);
    CURSOR c1 IS SELECT distinct x rec FROM test_table order by x;
    CURSOR c2 IS SELECT distinct y rec FROM test_table order by y;
begin

     -- for disply header in y  
     for y in c2 loop
       HEAD := HEAD || lpad(y.rec,4);
     end loop;
     DBMS_OUTPUT.put_line( lpad('X',3) || HEAD );
    --

    -- disply value with repect to x and y
    for x in c1 loop
        declare
        STR VARCHAR2(100);
        CURSOR c2 IS SELECT svalue rec FROM test_table where x= x.rec order by y;
        begin
          for y in c2 loop
           STR := str || lpad(y.rec,4);
          end loop;
          DBMS_OUTPUT.put_line(lpad(x.rec,3) || STR);
        end;
    end loop;
    --
end;

我希望这会有所帮助。

答案 1 :(得分:1)

如果我们想获得笛卡尔坐标系的枢轴输出

在脚本下运行以创建枢轴功能 http://paste.ubuntu.com/21378705/

pivot函数实际上是在将上面的脚本运行查询破坏为

之后给出动态列输出的引用游标
select * from table(pivot('select x,y,svalue from test_table'))

在上面的查询select语句中使用如下方式

select rowsection,columnsection,data from table     

我希望这会有所帮助。