Reputation: 181
I have a table like this:
Sequence ID Date other fields
1 23 2012-01-01
2 23 2012-02-03
3 23 2012-02-02
4 45 2012-01-01
5 45 2012-01-02
6 52 2012-01-01
7 52 2012-03-01
.. ...
I need a query that returns me rows that have inversed dates(there is a higher sequence with an older date
in example abow it should return rowq with sequence 2 and 3. For id 23, Seq 2 has date newer that sequence no 3.
Thanks
Upvotes: 2
Views: 1707
Reputation: 24046
try this:
select Sequence,ID,Date
from
( select *,
ROW_NUMBER() over (partition by ID order by date) as row_num_date,
ROW_NUMBER() over (partition by ID order by Sequence) as row_num_seq
from your_table )a
where row_num_date!=row_num_seq
Upvotes: 1
Reputation: 7267
select t1.*
from <table> t1
join <table> t2
on t1.Id=t2.Id
and (((t1.Sequence>t2.Sequence) and (t1.Date<t2.Date)) or
((t1.Sequence<t2.Sequence) and (t1.Date>t2.Date)))
And the fiddle for it http://sqlfiddle.com/#!3/beaf8/2/0
Upvotes: 3
Reputation: 10118
select * from
(select T1.Sequence as S1, T2.Sequence as S2, T1.ID
from T as T1, T as T2
where T1.Sequence < T2.Sequence
and T1.Date > T2.Date
and T1.ID = T2.ID) Subq
inner join T on (T.ID = Subq.ID
and (T.Sequence = Subq.S1 or T.Sequence = Subq.S2))
Upvotes: 1