我有如下数据:
act_desc CustNO Date_Time VASPK
GPRS Weekly Bundle 3012201312 2013-10-19 21:48:10.000 9588288
GPRS Weekly Bundle 3012201312 2013-10-19 21:48:03.000 9588286
GPRS Monthly Bundle 3012201312 2013-10-19 21:47:54.000 9588283
GPRS Monthly Bundle 3012201312 2013-10-19 21:47:33.000 9588281
GPRS Monthly Bundle 3012201312 2013-10-19 21:46:55.000 9588280
GPRS Monthly Bundle 3012201312 2013-10-19 21:46:43.000 9588278
GPRS Monthly Bundle 3012201312 2013-06-05 18:31:33.000 8582897
GPRS Monthly Bundle 3012201312 2013-05-04 20:18:54.000 8351398
GPRS Monthly Bundle 3012201312 2013-05-04 18:40:46.000 8350314
GPRS Monthly Bundle 3012201312 2013-05-04 18:40:25.000 8350308
GPRS Monthly Bundle 3012201312 2013-03-24 23:47:56.000 8069341
GPRS Monthly Bundle 3012201312 2013-03-24 23:47:42.000 8069340
GPRS Monthly Bundle 3012201312 2013-03-24 23:47:09.000 8069338
我想根据最近30天的最短日期时间选择记录,即我想选择日期时间并返回30天以检查是否存在相同记录,如果存在则选择最小记录否则选择一个可用的。
答案 0 :(得分:0)
要从今天起30天后获取日期,您可以使用dateadd函数:
select dateadd(day, -30, getdate())
并根据查询后的给定条件选择最小日期将起作用:
SELECT Min (date_time)
FROM yourtable
WHERE CONVERT(DATE, date_time) >= CONVERT (DATE, Dateadd(day, -30, Getdate()))
AND CONVERT(DATE, date_time) <= CONVERT (DATE, Getdate())