I need to get the row with the maximum date by the ItemKey
.
I need the whole row.
I have this table:
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
I need to get this:
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
I tried this SQL statement:
select *
from MyTBL
where Tdate = (select MAX(Tdate) from MyTBL)
But unfortunately it does not work
Thanks
4条答案
按热度按时间gdrx4gfi1#
you can use
ROW_NUMBER
to achieve thisor in another way (using common table expressions)
pes8fvy92#
Just try like this;
vxbzzdmp3#
SQL HERE
You can use this :
jslywgbw4#
Try this
SQL fiddle