27,580
社区成员
发帖
与我相关
我的任务
分享
呵呵!
抢走了!
date name sell
10-04 AA 11
09-23 BB 11
10-31 CC 32
这里的date应该是字符串,不是时间吧。
cast((year(getdate()+date)) as smalldatetime)--转成时间 ,加上年份,默认为当年
select cast((year(getdate()+date)) as smalldatetime) as curDate,name,sell from tb
select t.* from (select cast((year(getdate()+date)) as smalldatetime) as curDate,name,sell from tb) t where curdate = (select max(a.curdate) from (select cast((year(getdate()+date)) as smalldatetime) as curDate,name,sell from tb) as a where a.name = t.name)
select t.* from (select cast((year(getdate()+date)) as smalldatetime) as curDate,name,sell from tb) t where not exists (select 1 from (select cast((year(getdate()+date)) as smalldatetime) as curDate,name,sell from tb) as a where a.name = t.name and a.curdate > t.date)
select a.[date],a.[name],b.sell from
(select max(date) [date],[name] from tb
group by [name]) a,(select * from tb)b
where a.date=b.date and a.name=b.name
SELECT * FROM TB T1
WHERE NOT EXISTS(
SELECT 1 FROM TB T2 WHERE T2.NAME=T1.NAME AND T2.DATE>T1.DATE
)
select * from (select rn=row_number()over(partition by name order by date desc),* from tb) t where rn=1
--除了1楼的方法,还可以这样
select *
from tb t
where [date]=(select max([date]) from tb where name=t.name)
--或者
select *
from tb t
where [date]=(select top 1 [date] from tb where name=t.name order by [date] desc)
select t.* from tb t where date = (select max(date) from tb where name = t.name)
select t.* from tb t where not exists (select 1 from tb where name = t.name and date > t.date)
SELECT * FROM TB T1
WHERE NOT EXISTS(
SELECT 1 FROM TB T2 WHERE T2.NAME=T1.NAME AND T2.DATE>T1.DATE
)