如何在SYBASE中获取最小日期的记录

时间:2014-05-08 17:19:46

标签: sql tsql sybase

如何在SYBASE

中拾取带有较小日期的记录

我使用以下查询;

select count(id) as total, 
       convert(date, my_dte, 101) as new_dte
from   my_table
where  convert(date, my_dte, 101) between '05/01/2014' and '05/31/2014'
group by convert(date, my_dte, 101)
order by convert(date, my_dte, 101)

这是此查询的输出

**total        new_dte**
73             05/02/2014
14             05/06/2014
90             05/19/2014
36             05/21/2014

我想只拿起最小的日期记录。我正在使用此查询;

select max_rec.total
from 
(
select count(id) as total, 
       convert(date, my_dte, 101) as new_dte,
       ROW_NUMBER () OVER(PARTITION BY total ORDER BY new_date DESC ) as rn    
from   my_table
where  convert(date, my_dte, 101) between '05/01/2014' and '05/31/2014'
group by convert(date, my_dte, 101)
order by convert(date, my_dte, 101)
) max_rec
where max_rec.rn = 1;

但是这不能解决它在 OVER 时出错。

期望的输出:

**total        new_dte**
73             05/02/2014

任何帮助都会得到满足。

1 个答案:

答案 0 :(得分:0)

你能不能

将原始查询包装为子查询

创建另一个带回最小日期值的子查询

对两个子查询执行内部联接,以从原始查询中获取与所需日期匹配的记录