thomas.mac
thomas.mac

Reputation: 1256

Filter SQL column based on value of another column

Here is a SQL table "OptionValue" :

t_date      strike    
1/1/2011       89        
1/1/2011       105
1/2/2011       70
1/2/2011       82

and another table "PriceValue":

t_date        price    
1/1/2011      100        
1/2/2011      93

What I'm wanting to do, is select every row where the strike is < than price * (.9) , for that specific t_date ... eg.

t_date     price    strike      limit
1/1/2011   100       89         (100*.9 = 90) , 89 < 100
1/2/2011   93        70         (93*.9 =83.7) , 70 < 83.7
1/2/2011   93        82         (93*.9 =83.7) , 82 < 83.7 

Note: every t_date has a different price (diff. limit), is it possible to apply a specific filter, for each trade date?

Upvotes: 2

Views: 1371

Answers (1)

petre
petre

Reputation: 1543

select * from option_value ;

  t_date  | price | strike 
----------+-------+--------
 1/1/2011 |   100 |     89
 1/1/2011 |       |    105
 1/2/2011 |    93 |     95
 1/2/2011 |       |     82
(4 rows)

So you need to compare each row to rows with same t_date:

select * from option_value v1 join option_value v2 on v1.t_date=v2.t_date where v2.strike < (v1.price * 0.9);

  t_date  | price | strike |  t_date  | price | strike 
----------+-------+--------+----------+-------+--------
 1/1/2011 |   100 |     89 | 1/1/2011 |   100 |     89
 1/2/2011 |    93 |     95 | 1/2/2011 |       |     82
(2 rows)

Upvotes: 2

Related Questions