Jinru
Jinru

Reputation: 1

Comparing two rows in SQL

I want to compare two rows in a table. Retrieve record where line 2 value for a particular year is less than the line 1 value for the same year:

Year Line    Dollar
2001  1          $50
2001  2          $50

2002  1          $100
2002  2          $100

2003  1          $150
2003  2          $100

The result is

Year Line    Dollar
2003  1          $150
2003  2          $100

Thanks

Upvotes: 0

Views: 510

Answers (1)

bob
bob

Reputation: 390

select a.*, b.*
from yourtable a, yourtable b
where a.year = b.year
and a.line = 1
and b.line = 2
and a.dollar > b.dollar

Upvotes: 2

Related Questions