我有一个数据表(db是MSSQL):输入日期:01-09-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
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
谢谢!
答案 0 :(得分:0)
试试这个..
select ID, OrderNO, PartCode, FundId, Quantity, lastmodified from Table where lastmodified = (select max(lastmodified) from Table where lastmodified < '01-09-2014')
答案 1 :(得分:0)
试试这个:
select * from yourtable where lastmodified = (select max(lastmodified) from yourtable where lastmodified < inputdate)
答案 2 :(得分:0)
以下查询将显示给定输入日期后两天的结果差异:
注意:根据您的要求将数字2更改为任何数字。
select * from table where lastmodified between DATEADD(day, -1, inputdate) AND DATEADD(day, -2, inputdate)
答案 3 :(得分: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;