2017-09-22 162 views
0

我想选择一个日期列中最古老的日期值。选择最早的日期

例如: 我想得到最早日期的30%。如果我有10个寄存器,我想要得到的底部/最古老3.

例2:

如果: 约翰自2005年以来 吉娜工作自2003年以来 马克一直工作一直工作自2000年以来 楼一直致力于自2015年

我想马克和吉娜,因为他们已经在公司工作更长的时间。

+1

不知道你是什么意思,请张贴一些样本数据和预期结果。基本上,您可以使用TOP,只需将'ORDER BY'从'DESC'更改为'ASC',或以其他方式获取底部 – Squirrel

+0

请将示例数据和所需输出添加到您的问题。 –

+0

我认为这种方式更“可以理解”。 –

回答

1

基本上你还在用SELECT TOP声明,只是改变了ORDER BYASCDESC按升序或降序返回结果

这里有一些简单的查询来说明

-- Create a Sample Table 
declare @sample table 
(
    date_col date 
) 

-- Insert some sample dates 
insert into @sample select getdate() 
insert into @sample select getdate() - 1 
insert into @sample select getdate() - 2 
insert into @sample select getdate() - 3 
insert into @sample select getdate() - 4 
insert into @sample select getdate() - 5 

-- Get TOP 3 rows order by date in ascending (oldest to latest) 
select top 3 * 
from @sample 
order by date_col 

-- Get TOP 3 rows order by date in descending (latest to oldest) 
select top 3 * 
from @sample 
order by date_col desc 

-- Get TOP 30 percent, total 6 rows so 30% is 2 rows in ascending order 
select top (30) percent * 
from @sample 
order by date_col 

-- in descending order 
select top (30) percent * 
from @sample 
order by date_col desc 
+1

。有时候,小事情会让你停下太久的时间。谢谢!完美工作。这个为我工作: select top(30) –