查询按日期显示汇率

时间:2016-03-11 17:19:43

标签: sql oracle

如果这是一个愚蠢的问题,请道歉......

我有一张表,在不同日期持有多种货币的汇率,如下所示:

+-----------------------------+
| FOREKEY  FOREDATE  FORERATE |
+-----------------------------+
| 1        01/01/16  1.5      |
| 2        01/01/16  1.9      |
| 3        01/01/16  9.2      |
| 4        01/01/16  1.0      |
| 2        01/02/16  1.7      |
| 3        01/03/16  9.0      |
| 4        01/04/16  1.1      |
+-----------------------------+

我想创建一个查询,给出任何给定日期的现行货币汇率。

我已尝试过以下查询,但这并未显示例如01/03/16的FOREKEY 1汇率。

SELECT
   F.FOREDATE,
   F.FOREKEY,
   F.FORERATE
FROM
  FORERATE F
       INNER JOIN 
        (SELECT
            MAX(FOREDATE) FOREDATE
        FROM 
            FORERATE 
         GROUP BY 
            FOREDATE) FSUB
        ON (F.FOREDATE  = FSUB.FOREDATE) OR (F.FOREDATE > FSUB.FOREDATE)

感激不尽的任何帮助!

1 个答案:

答案 0 :(得分:3)

您可以在where子句中使用SELECT MAX(),如下所示:

SELECT
   F.FOREDATE,
   F.FOREKEY,
   F.FORERATE
FROM FORERATE F
WHERE F.FOREDATE = (SELECT MAX(FOREDATE) 
                    FROM FORERATE FMD 
                    WHERE F.FOREKEY=FMD.FOREKEY)