I have following table 

```
sec_id|pri_id   |date_col|  
abc|    1   |28-02-2017|    
bcd|    1   |09-01-2017|    
ef| 1   |   |
ghi|    2   |04-04-2017|    
jkl|    2   |05-05-2016|

```

If sec\_id belong to same pri\_id then select sec\_id with oldest date and give it value 1 and other zero.

output something like this 
[output table](https://i.stack.imgur.com/9kwtW.png)

```
|sec_id |pri_id |date_col  |new_column |
|abc    |1  |28-02-2017|    0|
|bcd    |1  |09-01-2017|    1|
|ef |1  |      |    0|
|ghi    |2  |04-04-2017|    0|
|jkl    |2  |05-05-2016|    1|

select f_id, s_id, case when min(cast(date_id as timestamp)) then 1 else 0 end as new_column from test_sc group by f_id,s_id. 

```

Plus there are other conditions too , if date\_col null then take col\_e ... if col\_e =r then 1 as new new\_col ,if col\_e is null then take col\_f ..if col\_f with lowest value ..like wise 6 more conditions. but meanwhile just date\_col .