2016-11-08 16 views
3

私はテーブルAを持っているOracleのクエリを書き込もうとしていますが、私は転送のコースを探しています。この要件では、JanデータとFebデータを比較し、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 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; 

答えて

2

オラクルには機能遅れがあります。

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; 

EDIT

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; 
+0

今、私はそれの日付の異なる日付の順序でテーブル内の複数のIDのために、このクエリを更新する必要があります。 – Qasim0787

+0

@ Qasim0787でこのアドバイスをお願いしますが、このクエリはテーブル内のすべての日付で機能します。だから何が問題なの? – Kacper

+0

私は2つのidを例として追加しました。テーブルを作成して試してみると、私は何の問題が起きているのか分かります。 – Qasim0787

関連する問題