【发布时间】:2017-12-11 19:03:48
【问题描述】:
我需要通过ItemKey 获取最大日期的行。
我需要整行。
我有这张桌子:
num | ItemKey | Serial | Qty | ItemName | Tdate
----+---------+--------+-----+----------+-------------------------
1 | 111 | 5 | 10 | AAA | 2010-03-25 00:00:00.000
2 | 111 | 0 | 12 | AAA | 2010-03-26 00:00:00.000
3 | 222 | 6 | 13 | BBB | 2010-03-25 00:00:00.000
4 | 222 | 2 | 11 | BBB | 2010-03-28 00:00:00.000
5 | 333 | 3 | 15 | CCC | 2010-03-25 00:00:00.000
6 | 333 | 4 | 16 | CCC | 2010-03-26 00:00:00.000
7 | 333 | 0 | 17 | CCC | 2010-03-27 00:00:00.000
我需要得到这个:
num | ItemKey | Serial | Qty | ItemName | Tdate
----+---------+--------+-----+----------+--------------------------
2 | 111 | 0 | 12 | AAA | 2010-03-26 00:00:00.000
4 | 222 | 2 | 11 | BBB | 2010-03-28 00:00:00.000
7 | 333 | 0 | 17 | CCC | 2010-03-27 00:00:00.000
我试过这条 SQL 语句:
select *
from MyTBL
where Tdate = (select MAX(Tdate) from MyTBL)
但不幸的是它不起作用
谢谢
【问题讨论】:
-
如果有多个MAX(Date)记录怎么办?它应该返回一个数据集还是需要进一步的选择要求。
标签: sql sql-server tsql transactions