gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 01:20:33 25 4
gpt4 key购买 nike

我正在尝试在 oracle 中编写一个查询,我有一个表 A,我想在类(class)中找到 Transfer。在此要求中,我必须将 2 月的数据与 1 月的数据进行比较,并生成包含 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

要求:如果 course1 <> 上个月 course1 或 类(class) 2 <> 上个月类(class) 2 然后是 否则 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;

最佳答案

在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;

关于sql - 如何将查询中的日期与上个月的日期进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40495654/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com