2014-10-28 79 views
0

我有数据表(db是MSSQL):输入日期:2014年1月9日获取记录具有接近日期定输入日期

ID OrderNO PartCode FundId Quantity lastmodified 
417 2144  44917  A  100  31-08-2014 
418 7235  11762  B  5  31-08-2014 
419 9991  60657  C  100  31-08-2014 
420 9992  60658  D  90  31-08-2014 
421 9993  60659  A  100  31-07-2014 
422 9994  60660  B  90  31-07-2014 
421 9993  60659  C  100  31-07-2014 
422 9994  60660  D  90  31-07-2014 

我想提出一个返回查询记录集合,但仅限于上次修改的日期,与给定的输入日期相比更小。

从示例表,我想找回以下信息:

ID OrderNO PartCode FundId Quantity lastmodified 
417 2144  44917  A  100  31-08-2014 
418 7235  11762  B  5  31-08-2014 
419 9991  60657  C  100  31-08-2014 
420 9992  60658  D  90  31-08-2014 

谢谢!

+2

密切关系较小意味着差异多少天? – knkarthick24 2014-10-28 08:55:46

+0

他的意思是“最接近”...... – SmartDev 2014-10-28 10:58:52

回答

0

试试这个..

select ID, OrderNO, PartCode, FundId, Quantity, lastmodified from Table where lastmodified = (select max(lastmodified) from Table where lastmodified < '01-09-2014') 
0

试试这个:

select * from yourtable where lastmodified = (select max(lastmodified) from yourtable where lastmodified < inputdate) 
0

下面的查询会给你从给定输入日期两天的结果差异:
注:更改号码2到任何数量根据您的req。

select * from table where lastmodified between DATEADD(day, -1, inputdate) AND DATEADD(day, -2, inputdate) 
+1

@GarethD:问题第一行说db是MSSQL。 – knkarthick24 2014-10-28 09:11:25

+0

我的不好,看到了现在已被删除的MySQL标签。 – GarethD 2014-10-28 09:12:53

+0

@GarethD:通过你学习了上述答案的MySQL语法。我应该感谢你。 – knkarthick24 2014-10-28 09:17:51

0

这将返回某个日期之前每个FundId的上次更新;

with cte as 
(
    select 
    *, 
    row_number() over(partition by FundId order by lastmodified desc) as rn 
    from @t where lastmodified < '2014-09-01' 
) 
select * 
from cte 
where rn = 1;