将列数据拆分为两列

时间:2016-07-20 05:04:50

标签: oracle jasper-reports

这是实际输出。

    DEPTNO ENAME             SAL
---------- ---------- ----------
        30 JAMES             950
        31 WARD             1250
        32 MARTIN           1250
        33 TURNER           1500
        34 ALLEN            1600
        35 BLAKE            2850

但是我希望将它分开给jasper报告。

    DEPTNO ENAME             SAL        DEPTNO ENAME             SAL
---------- ---------- ----------    ---------- ---------- ----------
        30 JAMES             950            31 WARD             1250
        32 MARTIN           1250            33 TURNER           1500
        34 ALLEN            1600            35 BLAKE            2850

请告诉我,我必须更改查询或jasper组件(我已经更改了设计)。

我正在使用oracle。 提前谢谢。

2 个答案:

答案 0 :(得分:0)

试试这个:

with a(DEPTNO, ENAME, SAL, rn) as (
    select a.*, rownum rn from (
        select 30, 'JAMES', 950 from dual union 
        select 31, 'WARD',  1250 from dual union 
        select 32, 'MARTIN',1250 from dual union 
        select 33, 'TURNER',1500 from dual union 
        select 34, 'ALLEN', 1600 from dual union 
        select 35, 'BLAKE', 2850 from dual 
        -- union another 1 row to show you, what if EMP > even number
        union 
        select 39, 'JACK',  3850 from dual) a
        ) 

select a1.deptno,
       a1.ename, 
       a1.sal,
       a2.deptno,
       a2.ename, 
       a2.sal
  from a a1 left join a a2 on a1.rn + 1 = a2.rn
   where mod(a1.rn,2) = 1

输出测试用例的其他一些数据:

    DEPTNO ENAME         SAL     DEPTNO ENAME         SAL
---------- ------ ---------- ---------- ------ ----------
        30 JAMES         950         31 WARD         1250 
        32 MARTIN       1250         33 TURNER       1500 
        34 ALLEN        1600         35 BLAKE        2850 
        39 JACK         3850                              

答案 1 :(得分:-1)

如果没有动态sql,你就无法做到这一点。可能的查询之一是:

with t(DEPTNO, ENAME ,   SAL) as (
select        30, 'JAMES',             950 from dual union all
select        31, 'WARD',             1250 from dual union all
select        32, 'MARTIN',           1250 from dual union all
select        33, 'TURNER',           1500 from dual union all
select        34, 'ALLEN',            1600 from dual union all
select        35, 'BLAKE',            2850 from dual union all
select        36, 'MIKE',             2850 from dual),
w as (select DEPTNO, ENAME , SAL ,mod(rownum,3) as rn, rownum as ct from t)
select  
  EXTRACTVALUE(DEPTNO_ENAME_SAL_XML,'/PivotSet/item[1]/column[1]') as DEPTNO1,
  EXTRACTVALUE(DEPTNO_ENAME_SAL_XML,'/PivotSet/item[1]/column[2]') as ENAME1,
  EXTRACTVALUE(DEPTNO_ENAME_SAL_XML,'/PivotSet/item[1]/column[3]') as SAL1,
  EXTRACTVALUE(DEPTNO_ENAME_SAL_XML,'/PivotSet/item[2]/column[1]') as DEPTNO2,
  EXTRACTVALUE(DEPTNO_ENAME_SAL_XML,'/PivotSet/item[2]/column[2]') as ENAME2,
  EXTRACTVALUE(DEPTNO_ENAME_SAL_XML,'/PivotSet/item[2]/column[3]') as SAL2 from w
    pivot xml(max(ct) for (DEPTNO, ENAME , SAL) in(any,any,any))
order by DEPTNO1

如您所见,我指定列名称。您可以编写自己的过程来生成列名,但Oracle中的最大列名是255