请查看此结果表,我想检索Invoice_Number
上sales_rep
的{{1}}的最后31-dec-2013
INVOICE_NUMBER INVOICE_DATE FK_SALES_REP_ID
519740000010026 12/31/13 10:14 AM 121
518210000010868 12/31/13 7:34 AM 91
519740000010050 12/31/13 2:29 PM 121
519920000010273 12/31/13 10:25 AM 94
514990000010269 12/31/13 10:51 AM 127
514990000010270 12/31/13 11:09 AM 127
505700000012330 12/31/13 12:12 PM 106
518210000010867 12/31/13 7:24 AM 91
结果表:
INVOICE_NUMBER INVOICE_DATE FK_SALES_REP_ID
518210000010868 12/31/13 7:34 AM 91
519920000010273 12/31/13 10:25 AM 94
519740000010050 12/31/13 2:29 PM 121
505700000012330 12/31/13 12:12 PM 106
514990000010270 12/31/13 11:09 AM 127
答案 0 :(得分:0)
你的解决方案可能看起来像这样。重要的是确保选择MAX(INVOICE_TIME),其中日期等于您想要的日期。
SELECT * FROM Your.Table
WHERE
INVOICE_DATE =
(SELECT MAX(INVOICE_DATE) FROM Your.Table
WHERE DATE(INVOICE_DATE) = '12/31/2013')
答案 1 :(得分:0)
由于您没有指定DBMS,因此这是标准SQL:
select *
from
(
select t.*,
row_number()
over (partition by FK_SALES_REP_ID
order by INVOICE_DATE desc) as rn
from tab as t
where cast(INVOICE_DATE as date) = '12/31/13'
) as dt
where rn = 1