3
我想在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
要求:如果course1 <>比上月course1或 course2 <>比上月course2 則Y 其他ň
預期結果:
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;
現在我需要在它的日期不同的日期序列的表來更新這個查詢多個ID。你可以請它的建議 – Qasim0787
@ Qasim0787但這個查詢適用於你在表中的所有日期。那麼有什麼問題? – Kacper
我加了兩個id作爲例子。如果你可以創建表並嘗試你會看到我得到什麼問題。 – Qasim0787