每个名称下面最新的价格。一张表?如何查询

yzaiml 2007-11-07 03:37:07
表Price
ID NAME Date BidPrice
1 aaa 2007-10-6 55
2 bbb 2007-02-6 50
3 aaa 2007-02-5 60
4 ccc 2007-02-5 35
5 ddd 2007-02-4 75
6 ccc 2007-02-4 40
7 ddd 2007-02-5 49

要查询出如下:
name date bidprice
bbb 2007-02-6 50
aaa 2007-02-5 60
ccc 2007-02-5 35
ddd 2007-02-5 49

也就是查询出每个名称下面最新的价格
谢谢。。。
...全文
62 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
pt1314917 2007-11-07
  • 打赏
  • 举报
回复

select * from price a where Date in(select max(Date) from price where name=a.name group by name)
fa_ge 2007-11-07
  • 打赏
  • 举报
回复
select * from Price a
where not exits(select 1 from Price where a.name=name and time>a.time)
yangjiexi 2007-11-07
  • 打赏
  • 举报
回复
declare @price table (id int identity(1,1),name varchar(10),date datetime,bidprice float)

insert into @price
select 'aaa','2007-10-06',55
union all
select 'bbb','2007-02-06',50
union all
select 'aaa','2007-02-05',60
union all
select 'ccc','2007-02-05',35
union all
select 'ddd','2007-02-04',75
union all
select 'ccc','2007-02-04',40
union all
select 'ddd','2007-02-05',49

select a.* from @price a,
(select name,max(date) date from @price
group by name) b
where a.name=b.name
and datediff(day,a.date,b.date)=0
hhwy_007 2007-11-07
  • 打赏
  • 举报
回复
seelct * from price a where not exists(select 1 from price b where a.name=b.name and a.date<b.date)
kuangdp 2007-11-07
  • 打赏
  • 举报
回复
select a.* from Price a,(select Name,max(Date) Date from Price group by Name) b where a.name = b.name order by a.name
yzaiml 2007-11-07
  • 打赏
  • 举报
回复
no body?

22,199

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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