Query comparing two fields from two different records

0

I need to make a query in a table that compares the value of a field in the table with the value of the same field in the table in another record.

If the table is something like this:

| Contrato   | Oper  | Importe |
+------------+-------+---------+
| 00002158   | B234  | -10.00  |
+------------+-------+---------+
| 00002158   | B217  | 120.00  |
+------------+-------+---------+
| 00002158   | B173  | 100.00  |
+------------+-------+---------+

You would need to find those records where, for the same contract, the amount of the operation B217 is greater than the amount of the operation B173 . You should return the last two records.

Thanks and best regards

    
asked by Albert_0 17.09.2018 в 10:53
source

1 answer

0

I do not know if this is what you need, but a simple selection

select * from tabla where contrato like '00002158' and importe>(select top 1 t.Importe from tabla t where t.Contrato=tabla.contrato and t.Oper like 'B173')

    
answered by 17.09.2018 в 13:49