-----------------------------------------------
Value ISRepeat Changed
------------------------------------------------
666-66-8888 NULL 2011-10-19 15:28:47.310
555-55-6666 NULL 2011-10-19 15:27:26.413
444-44-5555 NULL 2011-10-19 15:25:02.660
333-33-4444 NULL 2011-10-19 15:23:57.120
222-22-3333 NULL 2011-10-19 15:13:57.903
222-22-2222 NULL 2011-10-19 15:13:03.517
100-10-2222 NULL 2011-10-19 15:07:52.010
100-10-1111 NULL 2011-10-19 15:06:59.690
我有一张这样的桌子。我想打印值
555-55-6666 NULL 2011-10-19 15:27:26.413
这是基于TIME(更改列)的表格中的最后一行但是....条件是最近值之前的时间。我怎么能这样做?
答案 0 :(得分:3)
最近的时间是:
SELECT MAX(Changed) AS most_recent FROM AnonymousTable
您希望最大时间小于此行:
SELECT MAX(Changed) AS second_latest
FROM AnonymousTable
WHERE Changed < (SELECT MAX(Changed) AS most_recent FROM AnonymousTable)
因此,您选择相关的行:
SELECT *
FROM AnonymousTable
WHERE Changed =
(SELECT MAX(Changed) AS second_latest
FROM AnonymousTable
WHERE Changed < (SELECT MAX(Changed) AS most_recent FROM AnonymousTable)
)
可能有一种更简洁的方法可以做到这一点,而且这种技术并不能很好地概括为第三或第四个最新值。 OLAP功能很可能有所帮助。
答案 1 :(得分:1)
试试这个:
select top 1 *
from YourTable
where Changed not in
(
select max(Changed)
from YourTable
)
order by Changed desc
或者:
select *
from
(
select ROW_NUMBER() over (order by Changed desc) as row_num,
*
from YourTable
) a
where row_num = 2