lundi 9 novembre 2015

How to select data in MySQL from the same table based on conditions and avoid duplicates?

I have read related answers, but I did not find a solution to my problem. Please help me with the following issue: I have two tables and I want to match them based on tenors, but considering the fact that in one table I can have different entries satisfying this condition. In the table from which I want to extract the information (inst), I have several data regarding the interest rates, all structured on different lines, e.g. I can either have LIBOR or OIS data. I would like to do the following query: to match the tenors from the second table (stgy) with the data from the first table (inst) for the OIS where available, and otherwise, to have the LIBOR data. Below a code that I tried and did not work:

SELECT  CASE
    WHEN stgy.tenor IN ('1D','1W','2W','3W','1M','2M','3M','4M','5M','6M','7M','8M','9M','10M','11M')
    THEN ( CASE WHEN security_type='OIS' THEN
                (SELECT price_last FROM inst WHERE currency = @base_crncy AND trade_date = @date_of_interest AND tenor = stgy.tenor)
            ELSE
                (SELECT price_last
               FROM inst                                                                                  
              WHERE security_type IN  ('LIBOR') AND currency = @base_crncy AND trade_date = @date_of_interest
            ) 
            END AS rate 
        )
       ELSE NULL
END AS int_rate

I am also matching on the same trading day and currency. Please consider that I can have other security_type as well. Thank you for your help!

Aucun commentaire:

Enregistrer un commentaire