Oracle-如何选择最近六个月的每个星期五?

时间:2020-01-22 07:12:57

标签: sql oracle

我正在处理货币表。过去六个月中,我需要每个星期五。我的查询选择两个日期之间的每一天。这是我的查询

SELECT valid_from,
       currency_code,
       currency_rate
 FROM currency_rate
 WHERE valid_from > add_months(sysdate,-6) --Selects every day
 AND currency_code IN ('USD', 'EUR','CAD','GBP','JPY') 
 AND currency_type IN ('MBDS')
 ORDER BY currency_code,valid_from;

输出

1   23/07/2019  CAD 4.3561
2   24/07/2019  CAD 4.3368
3   25/07/2019  CAD 4.3636
4   26/07/2019  CAD 4.3539
5   27/07/2019  CAD 4.3059
6   28/07/2019  CAD 4.3059
7   29/07/2019  CAD 4.3059
8   30/07/2019  CAD 4.2849
9   31/07/2019  CAD 4.2429
10  01/08/2019  CAD 4.214
..  ../../....  CAD ......
..  ../../....  CAD ......
..  ../../....  CAD ......
173 21/01/2020  CAD 4.5281

输出(应该是)

1   26/07/2019  CAD 4.3561
2   02/08/2019  CAD 4.3368
3   09/08/2019  CAD 4.3636
4   16/08/2019  CAD 4.3539
5   23/08/2019  CAD 4.3059
6   ..........  CAD ......
7   ..........  CAD ......
..  ..........  CAD ......
..  ..........  CAD ......
..  10/01/2020  CAD ......
..  17/01/2020  CAD ......

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

如果要查找Oracle或MS SQL Server,则不清楚。如果Oracle需要它,可以使用

WHERE valid_from > add_months(sysdate,-6)
   AND TO_CHAR(valid_from, 'Dy', 'nls_date_language = american') = 'Fri'