如何将查询中的日期与上个月的日期进行比较

时间:2016-11-08 20:17:32

标签: sql oracle oracle11g

我正在尝试在oracle中编写一个查询,我有一个表A,我想在课程中找到Transfer。在此要求中,我必须将Feb数据与Jan进行比较,并生成另一个具有Y或N结果的列。

ID     Date          Course1    Course2
123    01-JAN-2010   2000       3500
123    01-FEB-2010   2000       3500
123    01-MAR-2010   2500       3500
123    01-APR-2010   2000       3500
123    01-MAY-2010   2000       3000
123    01-JUN-2010   2000       3500

要求:如果课程1<>上个月的课程1或                 course2<>上个月的课程2              然后Y.              否则N

预期结果:

ID     Date          Course1    Course2   Transfer
123    01-JAN-2010   2000       3500      
123    01-FEB-2010   2000       3500      N
123    01-MAR-2010   2500       3500      Y
123    01-APR-2010   2000       3500      Y
123    01-MAY-2010   2000       3000      Y
123    01-JUN-2010   2000       3000      N

我的查询:

select a.ID, a.Date, a.Course1, a.Course2,

case when (a.Course1 <> (select b.Course1 from TableA b where b.Date = add_months(a.Date-1) and b.ID = a.ID )  or a.Course2 <> (select b.Course2 from TableA b where b.Date = add_months(a.Date-1) and b.ID = a.ID ) )
        then Y
    else N
end  as Transfer

from TableA a
where a.ID = '123';

但是这个查询我得到错误或无效的参数......

需要帮助来纠正查询或任何其他方法以获得所需的结果。

由于

添加------

如果您可以使用此数据,您将看到我遇到的问题

insert into TableA (idd, datee, course1, course2) values (123,'01-JAN-2010', 2000, NULL);
insert into TableA (idd, datee, course1, course2) values (123,'01-FEB-2010', 2000, NULL);
insert into TableA (idd, datee, course1, course2) values (123,'01-MAR-2010', 2000, NULL);
insert into TableA (idd, datee, course1, course2) values (123,'01-APR-2010', 2000, NULL);
insert into TableA (idd, datee, course1, course2) values (123,'01-MAY-2010', 2000, NULL);
insert into TableA (idd, datee, course1, course2) values (123,'01-JUN-2010', 2000, NULL);
insert into TableA (idd, datee, course1, course2) values (123,'01-JUL-2010', 2000, NULL);
insert into TableA (idd, datee, course1, course2) values (123,'01-AUG-2010', 2000, NULL);
insert into TableA (idd, datee, course1, course2) values (123,'01-SEP-2010', 2500, NULL);
insert into TableA (idd, datee, course1, course2) values (123,'01-OCT-2010', 2000, NULL);
insert into TableA (idd, datee, course1, course2) values (123,'01-NOV-2010', 2000, NULL);
insert into TableA (idd, datee, course1, course2) values (123,'01-DEC-2010', 2000, NULL);

insert into TableA (idd, datee, course1, course2) values (123,'01-JAN-2011', 2000, NULL);
insert into TableA (idd, datee, course1, course2) values (123,'01-FEB-2011', 2000, NULL);
insert into TableA (idd, datee, course1, course2) values (123,'01-MAR-2011', 2000, NULL);
insert into TableA (idd, datee, course1, course2) values (123,'01-APR-2011', 2000, NULL);
insert into TableA (idd, datee, course1, course2) values (123,'01-MAY-2011', 2000, NULL);
insert into TableA (idd, datee, course1, course2) values (123,'01-JUN-2011', 2000, NULL);
insert into TableA (idd, datee, course1, course2) values (123,'01-JUL-2011', 2000, NULL);
insert into TableA (idd, datee, course1, course2) values (123,'01-AUG-2011', 2000, NULL);
insert into TableA (idd, datee, course1, course2) values (123,'01-SEP-2011', 2500, NULL);
insert into TableA (idd, datee, course1, course2) values (123,'01-OCT-2011', 2000, NULL);
insert into TableA (idd, datee, course1, course2) values (123,'01-NOV-2011', 2000, NULL);
insert into TableA (idd, datee, course1, course2) values (123,'01-DEC-2011', 2000, NULL);

insert into TableA (idd, datee, course1, course2) values (123,'01-JAN-2012', 2000, NULL);
insert into TableA (idd, datee, course1, course2) values (123,'01-FEB-2012', 2000, NULL);
insert into TableA (idd, datee, course1, course2) values (123,'01-MAR-2012', 2000, NULL);
insert into TableA (idd, datee, course1, course2) values (123,'01-APR-2012', 2000, NULL);
insert into TableA (idd, datee, course1, course2) values (123,'01-MAY-2012', 2000, NULL);
insert into TableA (idd, datee, course1, course2) values (123,'01-JUN-2012', 2000, NULL);
insert into TableA (idd, datee, course1, course2) values (123,'01-JUL-2012', 2000, NULL);
insert into TableA (idd, datee, course1, course2) values (123,'01-AUG-2012', 2000, NULL);
insert into TableA (idd, datee, course1, course2) values (123,'01-SEP-2012', 2500, NULL);
insert into TableA (idd, datee, course1, course2) values (123,'01-OCT-2012', 2000, NULL);
insert into TableA (idd, datee, course1, course2) values (123,'01-NOV-2012', 2000, NULL);
insert into TableA (idd, datee, course1, course2) values (123,'01-DEC-2012', 2000, NULL);


insert into TableA (idd, datee, course1, course2) values (456,'01-JAN-2011', 2000, NULL);
insert into TableA (idd, datee, course1, course2) values (456,'01-FEB-2011', 2000, NULL);
insert into TableA (idd, datee, course1, course2) values (456,'01-MAR-2011', 2000, NULL);
insert into TableA (idd, datee, course1, course2) values (456,'01-APR-2011', 2000, NULL);
insert into TableA (idd, datee, course1, course2) values (456,'01-MAY-2011', 2000, NULL);
insert into TableA (idd, datee, course1, course2) values (456,'01-JUN-2011', 2000, NULL);
insert into TableA (idd, datee, course1, course2) values (456,'01-JUL-2011', 2000, NULL);
insert into TableA (idd, datee, course1, course2) values (456,'01-AUG-2011', 2000, NULL);
insert into TableA (idd, datee, course1, course2) values (456,'01-SEP-2011', 2500, NULL);
insert into TableA (idd, datee, course1, course2) values (456,'01-OCT-2011', 2500, NULL);
insert into TableA (idd, datee, course1, course2) values (456,'01-NOV-2011', 2500, NULL);
insert into TableA (idd, datee, course1, course2) values (456,'01-DEC-2011', 2500, NULL);

insert into TableA (idd, datee, course1, course2) values (456,'01-JAN-2012', 2500, NULL);
insert into TableA (idd, datee, course1, course2) values (456,'01-FEB-2012', 2500, NULL);
insert into TableA (idd, datee, course1, course2) values (456,'01-MAR-2012', 25000, NULL);
insert into TableA (idd, datee, course1, course2) values (456,'01-APR-2012', 2000, NULL);
insert into TableA (idd, datee, course1, course2) values (456,'01-MAY-2012', 2000, NULL);
insert into TableA (idd, datee, course1, course2) values (456,'01-JUN-2012', 2000, NULL);
insert into TableA (idd, datee, course1, course2) values (456,'01-JUL-2012', 2000, NULL);
insert into TableA (idd, datee, course1, course2) values (456,'01-AUG-2012', 2000, NULL);
insert into TableA (idd, datee, course1, course2) values (456,'01-SEP-2012', 2000, NULL);
insert into TableA (idd, datee, course1, course2) values (456,'01-OCT-2012', 2000, NULL);
insert into TableA (idd, datee, course1, course2) values (456,'01-NOV-2012', 2000, NULL);
insert into TableA (idd, datee, course1, course2) values (456,'01-DEC-2012', 2000, NULL);

insert into TableA (idd, datee, course1, course2) values (456,'01-JAN-2013', 2000, NULL);
insert into TableA (idd, datee, course1, course2) values (456,'01-FEB-2013', 2000, NULL);
insert into TableA (idd, datee, course1, course2) values (456,'01-MAR-2013', 2000, NULL);
insert into TableA (idd, datee, course1, course2) values (456,'01-APR-2013', 2000, NULL);
insert into TableA (idd, datee, course1, course2) values (456,'01-MAY-2013', 2000, NULL);
insert into TableA (idd, datee, course1, course2) values (456,'01-JUN-2013', 2000, NULL);
insert into TableA (idd, datee, course1, course2) values (456,'01-JUL-2013', 2000, NULL);
insert into TableA (idd, datee, course1, course2) values (456,'01-AUG-2013', 2000, NULL);
insert into TableA (idd, datee, course1, course2) values (456,'01-SEP-2013', 2500, NULL);
insert into TableA (idd, datee, course1, course2) values (456,'01-OCT-2013', 2500, NULL);
insert into TableA (idd, datee, course1, course2) values (456,'01-NOV-2013', 2500, NULL);
insert into TableA (idd, datee, course1, course2) values (456,'01-DEC-2013', 2500, NULL);

我的查询

select idd, datee, course1, course2, 
  case when (course1 <> lag(course1,1,course1) over (order by datee)) or
        (course2 <> lag(course2,1,course2) over (order by datee)) 
  then 'Y' else 'N' end as transfer
 from tableA 
  where idd in ('123', '456')
 order by idd, datee;

1 个答案:

答案 0 :(得分:2)

在oracle中存在功能滞后。

select id, date, course1, course2, 
  case when (course1 <> lag(course1,1,course1) over (order by date)) or
            (course2 <> lag(course2,1,course2) over (order by date)) 
  then 'Y' else 'N' end as transfer
  from tableA 
  order by date;

修改

select idd, datee, course1, course2, 
  case when (course1 <> lag(course1,1,course1) over (order by idd, datee)) or
        (course2 <> lag(course2,1,course2) over (order by idd, datee)) 
  then 'Y' else 'N' end as transfer
 from tableA 
  where idd in ('123', '456')
 order by idd, datee;