0

鉴于此表:

CREATE TABLE positions
   (    "EMP_ID" CHAR(10 BYTE), 
    "GTYPE" NUMBER, 
    "AMT" NUMBER, 
    "START_DATE" DATE, 
    "STOP_DATE" DATE
   )

这个数据:

 Insert Into positions (Emp_Id,Gtype,Amt,Start_Date,Stop_Date)
   select 'XA0022',1,1000,'01-MAY-2010','08-MAY-2012' from dual union
   Select 'XA0022',1,1000,'01-MAY-2010','31-DEC-2012' From Dual Union
   Select 'XA0022',2,500,'03-APR-2012','15-JUL-2012' From Dual Union
   Select 'XA0022',1,421,'01-MAY-2012','23-MAY-2012' From Dual Union
   Select 'XA0022',1,1514,'09-MAY-2012','31-DEC-2012' From Dual union
   select 'XA0022',1,600,'24-MAY-2012','24-MAY-2012' from dual;

我如何做到这一点:

from            to          type1   type2
   01-May-2010  02-Apr-2012     2000    0
   03-Apr-2012  30-Apr-2012     2000    500
   01-May-2012  07-May-2012     2421    500
   08-May-2012  08-May-2012     2421    500
   09-May-2012  22-May-2012     2935    500
   23-May-2012  23-May-2012     2935    500
   24-May-2012  15-Jul-2012     3114    500
   16-Jul-2012  31-Dec-2012     3014    0

注意:金额在 start_date 生效,在 stop_date 之后的第二天无效。

任何指点都感激不尽!

4

1 回答 1

0

使用 Oracle 的支点。

甲骨文枢轴

select * from
(select emp_id, gtype, amt, start_date, stop_date
from positions)
pivot (sum(amt) as amt for (gtype) in (1 as "TYPE1", 2 as "TYPE2"))
order by emp_id, start_date;

有了更多信息(谢谢),像这样?

select emp_id, gtype, start_date, 
case when next_amt <> amt then next_start_date -1 end as to_date
from
(select emp_id, gtype, start_date, amt,
lead(start_date,1) over (order by emp_id, start_date) next_start_date,
lead(amt,1) over (order by emp_id, start_date) next_amt
from
positions)
于 2012-05-28T01:53:54.517 回答