在SQL Server中查找最接近的日期

时间:2012-12-24 15:31:44

标签: sql sql-server date

我有dbo.X DateTime的表column Y,可能有数百条记录。

我的存储过程有参数@CurrentDate,我想查找上表column Ydbo.X的日期,该日期小于且最接近@CurrentDate.

如何找到它?

4 个答案:

答案 0 :(得分:60)

where子句将匹配日期小于@CurrentDate的所有行,并且由于它们是后续排序的,因此TOP 1将是与当前日期最接近的日期。

SELECT TOP 1 *
FROM x
WHERE x.date < @CurrentDate
ORDER BY x.date DESC

答案 1 :(得分:11)

使用DateDiff并根据该日期与输入内容之间的天数或秒数对结果进行排序

像这样的东西

    select top 1 rowId, dateCol, datediff(second, @CurrentDate, dateCol) as SecondsBetweenDates
    from myTable
    where dateCol < @currentDate
    order by datediff(second, @CurrentDate, dateCol)

答案 2 :(得分:-1)

我认为我有更好的解决方案。

我将展示一些图片来支持和解释最终的解决方案。

<强>背景 在我的解决方案中,我有一个外汇汇率表。这些代表不同货币的市场汇率。但是,我们的服务提供商在费率Feed方面存在问题,因此某些费率的值为零。我想用相同货币的汇率填补缺失的数据,这些汇率最接近缺失的汇率。基本上我想得到最接近的非零利率的RateId,然后我将替换。 (这在我的例子中没有显示。)

1)因此,首先让我们确定丢失的费率信息:

Query showing my missing rates i.e. have a rate value of zero

2)接下来让我们确定不缺少的费率。 Query showing rates that are not missing

3)这个查询是魔术发生的地方。我在这里做了一个假设,可以删除但添加它以提高查询的效率/性能。第26行的假设是我希望在缺失/零交易的同一天找到替代交易。 神奇的事情发生在第23行:Row_Number函数添加一个从1开始的自动编号,用于缺失和非缺失事务之间的最短时间差。下一个最接近的交易的rownum为2等。

请注意,在第25行,我必须加入货币,这样我就不会与货币类型不匹配。那就是我不想用CHF值替换澳元货币。我想要最接近的匹配货币。

Combining the two data sets with a row_number to identify nearest transaction

4)最后,让我们获取RowNum为1 的数据 The final query

查询完整查询如下;

    ; with cte_zero_rates as
(
        Select      * 
        from        fxrates
        where       (spot_exp = 0 or spot_exp = 0) 
),
cte_non_zero_rates as
(
        Select      * 
        from        fxrates
        where       (spot_exp > 0 and spot_exp > 0) 
)
,cte_Nearest_Transaction as
(
        select       z.FXRatesID    as Zero_FXRatesID
                    ,z.importDate   as Zero_importDate
                    ,z.currency     as Zero_Currency
                    ,nz.currency    as NonZero_Currency
                    ,nz.FXRatesID   as NonZero_FXRatesID
                    ,nz.spot_imp
                    ,nz.importDate  as NonZero_importDate
                    ,DATEDIFF(ss, z.importDate, nz.importDate) as TimeDifferece
                    ,ROW_NUMBER() Over(partition by z.FXRatesID order by abs(DATEDIFF(ss, z.importDate, nz.importDate)) asc) as RowNum
        from        cte_zero_rates z 
        left join   cte_non_zero_rates nz on nz.currency = z.currency
                    and cast(nz.importDate as date) = cast(z.importDate as date)
        --order by  z.currency desc, z.importDate desc
)
select           n.Zero_FXRatesID
                ,n.Zero_Currency
                ,n.Zero_importDate
                ,n.NonZero_importDate
                ,DATEDIFF(s, n.NonZero_importDate,n.Zero_importDate) as Delay_In_Seconds
                ,n.NonZero_Currency
                ,n.NonZero_FXRatesID
 from           cte_Nearest_Transaction n
 where          n.RowNum = 1
                and n.NonZero_FXRatesID is not null
 order by       n.Zero_Currency, n.NonZero_importDate

答案 3 :(得分:-6)

CREATE PROCEDURE CurrentDate
@CurrentDate DATETIME
AS
BEGIN
    Select * from orders
    where OrderDate < @CurrentDate
END
GO