找出最后日期的几列

muyebo 2010-08-17 05:27:09
date name sell
08-12 AA 12
09-13 AA 10
10-04 AA 11
06-12 BB 23
09-23 BB 11
10-31 CC 32
。。。
结果
date name sell
10-04 AA 11
09-23 BB 11
10-31 CC 32
...全文
234 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
abuying 2010-10-14
  • 打赏
  • 举报
回复
呵呵!
抢走了!
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)
  • 打赏
  • 举报
回复
大乌龟不过节啊。
kadboy 2010-09-29
  • 打赏
  • 举报
回复
Sql 2008 版

select [date],name,sell
from (select [date],name,sell,row=ROW_NUMBER() over(partition by Name order by [Date] desc) from @tb)
as tb where row=1
lcqtgb 2010-08-20
  • 打赏
  • 举报
回复
看我的。。还有一种

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
水族杰纶 2010-08-17
  • 打赏
  • 举报
回复
Y的
全流氓
都写差不多了
yingzhilian2008 2010-08-17
  • 打赏
  • 举报
回复
SELECT * FROM TB T1
WHERE NOT EXISTS(
SELECT 1 FROM TB T2 WHERE T2.NAME=T1.NAME AND T2.DATE>T1.DATE
)
htl258_Tony 2010-08-17
  • 打赏
  • 举报
回复
--2005的也可以:

select * from (select rn=row_number()over(partition by name order by date desc),* from tb) t where rn=1
百年树人 2010-08-17
  • 打赏
  • 举报
回复
--除了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)
dawugui 2010-08-17
  • 打赏
  • 举报
回复
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)
guguda2008 2010-08-17
  • 打赏
  • 举报
回复
假设你的表名叫TB
SELECT * FROM TB T1
WHERE NOT EXISTS(
SELECT 1 FROM TB T2 WHERE T2.NAME=T1.NAME AND T2.DATE>T1.DATE
)

27,580

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧