2017-09-22 186 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) –