如何在DATEADD()函数中引用2周 - 1天?

时间:2012-12-06 19:58:10

标签: sql sql-server sql-server-2008

我有一个数据集,显示相关工作日的货币即期汇率,并希望在每个货币的参考日期之前提取10个最新数据点的值。

数据如下:

AsOfDate    Currency    ExchangeRateUSD
9/11/1997   AED -1
9/11/1997   AUD 1.389178
9/11/1997   CAD 1.39125
9/11/1997   CHF 1.4774
9/11/1997   CZK 33.6755
9/11/1997   DKK 6.8203
9/11/1997   HKD 7.7455
9/11/1997   IDR 2943
9/11/1997   JPY 119.77
9/11/1997   KWD -1
9/11/1997   MXN 7.7855
9/11/1997   NOK 7.36715
9/11/1997   NZD 1.579654
9/11/1997   PHP 32.53
9/11/1997   SEK 7.7944
9/11/1997   SGD 1.511
9/11/1997   THB 35.475
9/11/1997   TWD 28.6125
9/11/1997   ZAR 4.698
9/12/1997   AED -1
9/12/1997   AUD 1.386001
9/12/1997   CAD 1.39325
9/12/1997   CHF 1.46475
9/12/1997   CZK 33.449
9/12/1997   DKK 6.7377
9/12/1997   HKD 7.746
9/12/1997   IDR 2933
9/12/1997   JPY 120.815
9/12/1997   KWD -1
9/12/1997   MXN 7.7765
9/12/1997   NOK 7.2844
9/12/1997   NZD 1.575175
9/12/1997   PHP 32.2
9/12/1997   SEK 7.65845
9/12/1997   SGD 1.514
9/12/1997   THB 36.225
9/12/1997   TWD 28.613
9/12/1997   ZAR 4.6865
9/15/1997   AED -1
9/15/1997   AUD 1.387829
9/15/1997   CAD 1.39185
9/15/1997   CHF 1.45175

参考日期为2/19/1999,我想返回以下数据(示例仅显示两种货币,返回的实际数据将包括所有货币):

    AsOfDate    Currency    ExchangeRateUSD
2/8/1999    EUR 0.888968
2/9/1999    EUR 0.884956
2/10/1999   EUR 0.883275
2/11/1999   EUR 0.885152
2/12/1999   EUR 0.886682
2/15/1999   EUR 0.889957
2/16/1999   EUR 0.894454
2/17/1999   EUR 0.890234
2/18/1999   EUR 0.891107
2/19/1999   EUR 0.901266
2/8/1999    JPY 113.71
2/9/1999    JPY 114.38
2/10/1999   JPY 114.52
2/11/1999   JPY 114.3
2/12/1999   JPY 114.305
2/15/1999   JPY 115.525
2/16/1999   JPY 118.175
2/17/1999   JPY 118.895
2/18/1999   JPY 119.82
2/19/1999   JPY 120.53

对于参考日期为2/25/1999,我想返回以下数据(同样,实际数据会显示所有货币):

AsOfDate    Currency    ExchangeRateUSD
2/12/1999   EUR 0.886682
2/15/1999   EUR 0.889957
2/16/1999   EUR 0.894454
2/17/1999   EUR 0.890234
2/18/1999   EUR 0.891107
2/19/1999   EUR 0.901266
2/22/1999   EUR 0.906454
2/23/1999   EUR 0.910705
2/24/1999   EUR 0.91295
2/25/1999   EUR 0.904895
2/12/1999   JPY 114.305
2/15/1999   JPY 115.525
2/16/1999   JPY 118.175
2/17/1999   JPY 118.895
2/18/1999   JPY 119.82
2/19/1999   JPY 120.53
2/22/1999   JPY 120.33
2/23/1999   JPY 121.195
2/24/1999   JPY 121.88
2/25/1999   JPY 120.1

如您所见,根据参考日期,开始日期和结束日期之间的天数会有所不同,因此我无法在DATEADD()中使用DATEPART日期。使用化合物2周 - 1天将获得适当的范围。

有没有办法在DATEADD()中使用复合DATEPART或其他方式来执行此操作?

编辑 - 我错过了一个关键点...我想同时为多个货币同时提供多个参考日期。我有一个参考日期表(#referenceDates),我将加入汇率表(#exchangeRates),我想在WHERE子句中使用BETWEEN为每个参考日期提取适当的日期,因此在DATEADD()中需要复合DATEPART。

3 个答案:

答案 0 :(得分:1)

参考日期为2/25/1999

SELECT TOP(10) *
  FROM DATA
 WHERE Currency = 'JPY'
   AND AsOfDate <= '19990225'
ORDER BY AsOfDate DESC;

作为参考,如果你真的想要2周 - 1天,那就是一个天真的表达:

DATEADD(D, +1, DATEADD(WK, -2, '19990225'))

但你最好直接去-13天

DATEADD(D, -13, '19990225')

或者在您的查询中(但如果数据中的天数存在差距,则不会为您提供10个数据点):

SELECT *
  FROM DATA
 WHERE Currency = 'JPY'
   AND AsOfDate <= '19990225'
   AND AsOfDate >= DATEADD(D, -13, '19990225')
ORDER BY AsOfDate ASC;

答案 1 :(得分:0)

看起来你可以逃脱:

select top 10 *
from YourTable
where AsOfDate <= @refDate
order by AsOfDate desc

答案 2 :(得分:0)

查询:

<强> SQLFIDDLEExample

SELECT
a.*
FROM(
SELECT TOP 10
t1.*
FROM tbl t1
WHERE t1.AsOfDate <='2/25/1999'
ORDER BY t1.AsOfDate DESC
)a
ORDER BY a.AsOfDate ASC

2/25/1999的结果:

|                        ASOFDATE | CURRENCY | EXCHANGERATEUSD |
----------------------------------------------------------------
| February, 12 1999 00:00:00+0000 |      JPY |         114.305 |
| February, 15 1999 00:00:00+0000 |      JPY |         115.525 |
| February, 16 1999 00:00:00+0000 |      JPY |         118.175 |
| February, 17 1999 00:00:00+0000 |      JPY |         118.895 |
| February, 18 1999 00:00:00+0000 |      JPY |          119.82 |
| February, 19 1999 00:00:00+0000 |      JPY |          120.53 |
| February, 22 1999 00:00:00+0000 |      JPY |          120.33 |
| February, 23 1999 00:00:00+0000 |      JPY |         121.195 |
| February, 24 1999 00:00:00+0000 |      JPY |          121.88 |
| February, 25 1999 00:00:00+0000 |      JPY |           120.1 |