简体   繁体   中英

sql - get max(date) before set date

I have table of results

2013-01-28 00:00:00 0001101 669.170000
2013-01-29 00:00:00 0001101 669.170000
2013-01-30 00:00:00 0001101 669.170000
2013-01-31 00:00:00 0001101 669.170000
2013-02-04 00:00:00 0001101 669.170000
2013-02-05 00:00:00 0001101 669.170000
2013-02-06 00:00:00 0001101 669.170000

If I have a date that is not in this result set, I need to get first date before for second column, to return third column

For example, for date 2013-02-03, I do not have result, so I need to get first before from 2013-01-31

Assuming your above result set is in tbl1 and you want to result and for SQL server dbms

SELECT TOP 1 *
FROM tbl1
WHERE  Date <= '2013-02-03' 
ORDER BY Date DESC

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM