多年来一年中最早的日期

时间:2014-09-07 13:36:53

标签: sql oracle

尝试设置一个查询,该查询返回多年内一年中最早的日期。

例如:

06-apr-1990
07-may-1991
03-apr-1992
07-jun-1993

最早的是03-apr-1992

感谢任何帮助( - :

我正在使用Oracle SQL Developer

日期采用日期格式

4 个答案:

答案 0 :(得分:1)

如果您使用的是SQL Server,请尝试:

SELECT TOP(1) date FROM table ORDER BY Month(date), Day(date)

对于MySQL,这应该可以解决问题:

SELECT date FROM table ORDER BY Month(date), Day(date) LIMIT 1;

对于Oracle:

SELECT date FROM table ORDER BY Month(date), Day(date) WHERE ROWNUM <= 1;

答案 1 :(得分:0)

尝试以下查询:

SELECT column_name,column_name FROM table_name ORDER BY column_name,column_name ASC|DESC;

来源w3school.

答案 2 :(得分:0)

这样的事情怎么样......

create table #temp1
(
 ID INT , 
 CreatedDate datetime
)

INSERT #temp1 (Id, CreatedDate )values(1, '06-apr-1990')
INSERT #temp1 (Id, CreatedDate )values(2, '07-may-1991')
INSERT #temp1 (Id, CreatedDate )values(3, '03-apr-1992')
INSERT #temp1 (Id, CreatedDate )values(4, '07-jun-1993')

SELECT A.CREATEDDATE FROM
(
    SELECT TOP 1 CREATEDDATE, CAST(YEAR(CreatedDate) as char(4)) [YEAR], DATENAME(MONTH,CreatedDate)   [MONTH]
    , DATENAME(DAY,CreatedDate) [DAY]
    FROM #temp1
    GROUP BY CreatedDate
    ORDER BY [DAY], [MONTH], [YEAR]
 ) A

答案 3 :(得分:0)

ANSI上支持

Oracle 12 SQL:

select * 
from tbl 
order by extract(month from the_date), extract(day from the_date)
fetch first 1 row only;

对于较低版本的Oracle,您可以使用:

with a as
(
  select 

      row_number() 
          over(order by 
               extract(month from the_date), extract(day from the_date)) as rn, 

      tbl.the_date

  from tbl 
)
select the_date 
from a
where rn = 1;

直播代码:http://sqlfiddle.com/#!4/e6a75/20